Search code examples
wpfsortingdatagridwpftoolkittoolkit

WPF Toolkit Datagrid Bug


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; }
    }
}

Solution

  • 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.