ItemsPanelTemplate Selector in wpf?

I need to set the ItemsPanelTemplate property from a list based on the dependency property of the control. How to use DataTemplateSelector for this?

I have something like:

<ListBox.ItemsPanel> <ItemsPanelTemplate> <!-- Here I need to replace with either a StackPanel or a wrap panel--> </ItemsPanelTemplate> </ListBox.ItemsPanel> 

thanks

+8
c # wpf
source share
2 answers

There is no ItemsPanelSelector (perhaps because it is not a DataTemplate ), but you can bind it or use Trigger

Binding example

 <ListBox ItemsPanel="{Binding RelativeSource={RelativeSource Self}, Path=Background, Converter={StaticResource MyItemsPanelConverter}}"> 

Trigger in Style example

 <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> 
+15
source share

I think the best route here would be to use a style for your ListBox and set triggers that change the ItemsPanel based on the DependencyProperty link that you are linking to.

0
source share

All Articles