我需要根据控件的依赖属性设置列表框的 ItemsPanelTemplate 属性。我如何使用 DataTemplateSelector 来做到这一点?
我有这样的东西:
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<!-- Here I need to replace with either a StackPanel or a wrap panel-->
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
谢谢
最佳答案
没有 ItemsPanelSelector
(可能是因为它不是 DataTemplate
),但您可以绑定(bind)它或使用 Trigger
绑定(bind)
示例
<ListBox ItemsPanel="{Binding RelativeSource={RelativeSource Self},
Path=Background,
Converter={StaticResource MyItemsPanelConverter}}">
样式
示例中的触发器
<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>
关于c# - wpf 中的 ItemsPanelTemplate 选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10652639/