views:

341

answers:

2

Hi

i have a tab control with 2 tabs. the content of each tab is binded to a ListCollectionView and the data template has a grid showing the items. The problem is that if you click on any of the columns to sort, if I select the other tab and return to the first tab the sorting is cleared. Is it a known bug?

here is the code:

<Window x:Class="WpfApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
Title="Window1" Height="538" Width="1223">
<Window.Resources>
    <DataTemplate x:Key="TestTemplate">
        <toolkit:DataGrid ItemsSource="{Binding}" AutoGenerateColumns="false" IsReadOnly="True" >
            <toolkit:DataGrid.Columns>
                <toolkit:DataGridTextColumn Binding="{Binding IntVal}" Header="Number"></toolkit:DataGridTextColumn>
                <toolkit:DataGridTextColumn Binding="{Binding StringVal}" Header="String"></toolkit:DataGridTextColumn>
            </toolkit:DataGrid.Columns>
        </toolkit:DataGrid>
    </DataTemplate>
</Window.Resources>
<Grid>
    <TabControl>
        <TabItem Header="tab1" ContentTemplate="{StaticResource TestTemplate}" x:Name="a" Content="{Binding}"/>
        <TabItem Header="tab2" ContentTemplate="{StaticResource TestTemplate}" x:Name="b" Content="{Binding}"/>
    </TabControl>
</Grid>

code behid:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        List<test> intt = new List<test> 
        {
            new test { IntVal = 5, StringVal = "abc" }, 
            new test { IntVal = 12, StringVal = "cc" }, 
            new test { IntVal = 2, StringVal = "sdgc" }, 
            new test{IntVal=64,StringVal="df"},
            new test{IntVal=1234,StringVal="werw"}, 
            new test{IntVal=14,StringVal="sdvvd"}, 
            new test{IntVal=136,StringVal="aasr"} 
        };

        List<test> intt2 = new List<test> 
        {
            new test { IntVal = 5, StringVal = "abc" }, 
            new test { IntVal = 12, StringVal = "cc" }, 
            new test { IntVal = 2, StringVal = "sdgc" }, 
            new test{IntVal=64,StringVal="df"},
            new test{IntVal=1234,StringVal="werw"}, 
            new test{IntVal=14,StringVal="sdvvd"}, 
            new test{IntVal=136,StringVal="aasr"} 
        };

        this.a.DataContext = new ListCollectionView(intt);
        this.b.DataContext = new ListCollectionView(intt2);
    }

    public class test
    {
        public int IntVal { get; set; }
        public string StringVal { get; set; }
    }
}
A: 

I think that the TabControl destroy and recreate its child every time you change the selected tab. That would explain why the sort is not kept.

You can check out this discussion on the WPF Disciples Google Group to have some infos about how to fix that problem.

Jalfp
ThanksI will give it a test and see if it helps.Although it does work when you hard code the datagrid in the tab. I guess that the tab creates everything from scratch only if you use a template
Amit
A: 

I had ran into a similar issue. I think wpf uses the same contentpresenter for data templates. In your case instance of the datagrid is the same, only data is rebound when you switch tabs. What I did was to create an instance of the control in codebehind and set it as a content property of the TabItem. e.g

TabItem ti = new TabItem();
ti.DataContext = intt;
ti.Content = new DataGrid();
myTabControl.Items.Add(t);

hope this helps..

netraju