I have a ListBox whose Style and ItemTemplate I change in code-behind on click of specific buttons.
listbox.ItemTemplate = FindResource("dataTemplateView1") as DataTemplate;
listbox.Style = FindResource("listBoxStyle1") as Style;
There are three possible views so there are three sets of data template and style. The DataTemplate contains some text and thumbnails (different sizes per set). The styles just change the ItemsPanelTemplate to WrapPanel, StackPanel (Horizontal) and StackPanel (Vertical). Example:
<Style x:Key="listBoxStyle1" TargetType={x:Type ListBox}">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
The problem is that when I have a selected item (say item with index 20) and I change the view, the visible area of the listbox shown will reset back to the first index (but the selected item is still selected, just not shown).
I tried to solve this using the solution here to scroll to the selected item and set it to center. But currently, there are times that the behavior seems choppy since what happens is that the first item in listbox is shown first then it jumps to the selected item. Any other alternatives for this?
Thanks!