我需要根据控件上的依赖属性来设置列表框的ItemsPanelTemplate属性。我该如何使用DataTemplateSelector来做到这一点?
我有一些类似的东西:
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<!-- Here I need to replace with either a StackPanel or a wrap panel-->
</ItemsPanelTemplate>
</ListBox.ItemsPanel>谢谢
发布于 2012-05-18 21:28:37
没有ItemsPanelSelector (可能是因为它不是DataTemplate),但您可以绑定它或使用Trigger
Binding示例
<ListBox ItemsPanel="{Binding RelativeSource={RelativeSource Self},
Path=Background,
Converter={StaticResource MyItemsPanelConverter}}">Style中的Trigger示例
<ListBox ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}">
<ListBox.Style>
<Style TargetType="ListBox">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<!-- Your Trigger.. -->
<Trigger Property="Background" Value="Green">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Style>
</ListBox>发布于 2012-05-18 21:28:05
我认为这里最好的方法是为你的ListBox使用样式,并设置触发器,根据你引用的DependencyProperty更改ItemsPanel。
https://stackoverflow.com/questions/10652639
复制相似问题