C# wpf中的ItemsPanelTemplate选择器?
我需要根据控件上的依赖项属性设置listbox的ItemsPanelTemplate属性。如何使用DataTemplateSelector来实现这一点 我有点像:C# wpf中的ItemsPanelTemplate选择器?,c#,wpf,C#,Wpf,我需要根据控件上的依赖项属性设置listbox的ItemsPanelTemplate属性。如何使用DataTemplateSelector来实现这一点 我有点像: <ListBox.ItemsPanel> <ItemsPanelTemplate> <!-- Here I need to replace with either a StackPanel or a wrap panel--> </ItemsPanelTempl
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<!-- Here I need to replace with either a StackPanel or a wrap panel-->
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
谢谢我认为最好的方法是为您的列表框使用样式,并设置触发器,根据您引用的DependencyProperty更改ItemsPanel。没有
ItemsPanelSelector
(可能是因为它不是数据模板
),但您可以绑定它或使用触发器
绑定
示例
太好了。谢谢你提供代码示例。这真的帮了我很大的忙。向我的朋友竖起大拇指。
<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>