我创建扩展的列表框这样的问题,单选按钮:Expander Auto Open/Close列表框扩展的不像个
解决方案的工作与在扩展内容时,列表框是窗口上的唯一项目。
但是,当我试图用我的自定义控件使用相同的代码,我得到这个错误:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ListBoxItem', AncestorLevel='1''. BindingExpression:Path=IsSelected; DataItem=null; target element is 'Expander' (Name=''); target property is 'IsExpanded' (type 'Boolean')
我已经尝试添加IsSelected属性在ListBox.ItemContainerStyle作为一个海报中建议另一个线程但失败了。
<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>
悲惨的失败了!!!!!
任何帮助表示赞赏:)
它是如何失败?你在屏幕上看到什么? – XAMeLi
扩展器应该是相互排斥的,就像分组的单选按钮。如果你点击一个,另一个打开,第一个应该关闭。如果用TextBlock替换嵌入在StackPanels中的UserControl(例如),它就可以工作。 –