I would like to have ListBox's ItemsPanelTemplate change during runtime.
I have the following XAML which allows me to change the ItemsPanelTemplate; however has the unwanted side effect of breaking the ScrollViewer.
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ie="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
...
<UserControl.Resources>
<ItemsPanelTemplate x:Key="StackPanelTemplate">
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
<ItemsPanelTemplate x:Key="WrapPanelTemplate">
<telerik:RadWrapPanel/>
</ItemsPanelTemplate>
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel>
<Button Content="StackPanel">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ie:ChangePropertyAction TargetName="TargetListBox" PropertyName="ItemsPanel" Value="{StaticResource StackPanelTemplate}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button Content="WrapPanel">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ie:ChangePropertyAction TargetName="TargetListBox" PropertyName="ItemsPanel" Value="{StaticResource WrapPanelTemplate}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
<ListBox x:Name="TargetListBox" Grid.Column="1" ItemsSource="{Binding SomeCollection}"/>
</Grid>
When you change the ItemsPanelTemplate this way. The ScrollViewer seems to stay in whatever state it was in before you changed it and using the scroll bar does not effect any change on the ListBox.
Can anyone provide any insight on this issue or perhaps provide a workaround?
Thank you.
* EDIT *
So, I've narrowed the problem down to it being related to virtualization. If you change the VirtualizingStackPanel for just a regular StackPanel the ScrollViewer does not break. This isn't really a solution for me though as this ListBox will hold many hundreds of search results.
I think the easiest workaround would be to replace entire ListBox instead of just the panel template.