I have a TabControl
where each tab contains a DataGrid
. When the user switches between tabs it seems that the DataGrid
is created from scratch. I say this becuase of three things that I've noticed: the columns are automatticaly recreated, the current sorted column is lost, and selection is lost.
I would love to be able to retain the current sorted view when the user returns to a tab page. They may be comparing lists and it doesn't make sense to have them resort each time. The databehind the scenes is not changing once it is created. However, the the tabs are bound to an observable collection because they need to be added/removed depending on how the application is being used.
ViewModel:
public ObservableCollection<DataTable> Tables
{
get
{
return _tables; // Tables are added/removed through secondary methods
}
}
View:
<TabControl ItemsSource="{Binding Path=Tables"}>
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultBinding StringFormat="{}{0} ({1})}>
<Binding Path="TableName"/>
<Binding Path="Rows.Count"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<DataGrid
AutoGeneratingColumns="True"
IsReadOnly="True"
ItemsSource="{Binding DefaultView}"
/>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
Is there anyway to keep the current sort order of the datagrid when tabs are changed?