If I have a datatemplate that will end up using a scrollbar in one
form or another, how can I retain the scrollbars position when the
datacontext changes back and forth?
I've read that you generally store the state of the UI in the
ViewModel. This makes sense for things like selected item, or is
expanded type states, but Im not sure how to apply it to the scrollbar
state. As in general, the scrollbar will automatically appear disappear.
Ive posted an example of what I mean below. When you select a tab and
move the scrollbar, then select another tab and move its scrollbar.
Upon returning to the first tab, the scrollbar has moved.
I believe this is due to the View being shared, and so it retains the percentage scrolled from the previous view?
<Window x:Class="WpfApplication13.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TabControl ItemsSource="{Binding Data}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Count}"/>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<ListBox ItemsSource="{Binding}"/>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</Window>
namespace WpfApplication13
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Data = new List<string[]>();
for (int i = 1; i < 10; ++i)
{
string[] strings = new string[i * 10];
for (int j = 0; j < i * 10; ++j)
{
strings[j] = new string('z', 100);
}
Data.Add(strings);
}
DataContext = this;
}
public List<string[]> Data
{
get;
set;
}
}
}






