この質問のようなエキスパンダーのリストボックスを作成しました: Expander Auto Open/Close
リストボックスがウィンドウ上の唯一のアイテムである場合、ソリューションはエキスパンダーのコンテンツで機能します。
しかし、カスタム コントロールで同じコードを使用しようとすると、次のエラーが発生します。
System.Windows.Data エラー: 4: 参照 'RelativeSource FindAncestor、AncestorType='System.Windows.Controls.ListBoxItem'、AncestorLevel='1'' を使用してバインディングのソースが見つかりません。BindingExpression:Path=IsSelected; DataItem=null; ターゲット要素は 'Expander' (Name='') です。ターゲット プロパティは 'IsExpanded' (タイプ 'Boolean') です
あるポスターが別のスレッドで提案したように、ListBox.ItemContainerStyle に IsSelected プロパティを追加しようとしましたが、失敗しました。
<ListBox Margin="5"
SelectionMode="Single"
ScrollViewer.VerticalScrollBarVisibility="Auto">
<ListBox.Resources>
<Style TargetType="{x:Type Expander}">
<Setter Property="IsExpanded"
Value="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" />
</Style>
<Style TargetType="{x:Type controls:SelectionCriteriaControl}">
<Setter Property="MaxHeight"
Value="200" />
</Style>
</ListBox.Resources>
<Expander Header="Fund Family" Margin="2">
<StackPanel>
<controls:SelectionCriteriaControl DataContext="{Binding FundFamilySelectionCriteria, Mode=TwoWay}" />
</StackPanel>
</Expander>
<Expander Header="Asset Class" Margin="2">
<StackPanel>
<controls:SelectionCriteriaControl DataContext="{Binding AssetClassSelectionCriteria, Mode=TwoWay}" />
</StackPanel>
</Expander>
<ListBox.Template>
<ControlTemplate TargetType="{x:Type ListBox}">
<ItemsPresenter />
</ControlTemplate>
</ListBox.Template>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ContentPresenter Content="{TemplateBinding Content}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
惨めに失敗!!!!!
どんな助けでも感謝します:)