【发布时间】:2017-04-26 06:45:01
【问题描述】:
正如标题所说,我想将我的 Expander 的 IsExpanded 属性绑定到 ItemsControl 的 Items.Count 属性,即 Expander 的内容。我需要更多扩展器的这种绑定,所以样式或类似的东西会很好。我已经尝试了一些在这里或其他网站上找到的东西,但没有任何效果。
这是我的实际代码:
<Style x:Key="ExpanderStyleKey" TargetType="Expander">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Content.Items.Count}" Value="0">
<Setter Property="IsExpanded" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
感谢您的帮助!
编辑:
这是我的扩展器之一的代码:
<Expander Name="exp1"
Header="Expander1"
Style="{StaticResource ExpanderStyleKey}">
<ItemsControl Name="itcMain"
ItemsSource="{Binding Path=ListInCodeBehind, Mode=OneWay}">
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Disabled">
<ItemsPresenter/>
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=ExampleProperty}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Expander>
【问题讨论】:
-
与 ItemsControl 共享 xaml,以便轻松检测绑定问题(如果有)。
-
你试过转换器吗? Count to Expanded 转换器在这种情况下很有用,它也可以重复使用
-
看起来问题出在绑定上。在这种情况下,不需要转换器,因为触发器看起来不错。
标签: wpf binding expander wpf-style