开发者

Sorting DataGrid on column value stored in Dictionary

Ok, I have been trying to figure this out for a while now and I am guessing that I am just lacking the correct terminology to actually find the solution. So please forgive me if this is a duplicate, but I have searched without any avail.

I have a collection of items that have a dictionary in them to hold other items. The dictionary is a string-object pair and I have used this to hold an instance of a class that I made. I created a simplified sample below.

public MainPage()
{
    InitializeComponent();
    List<Holder> holders = new List<Holder>()
    {
        new Holder()
        {
            Attributes = new Dictionary<string,object>()
            {
                {
                    "TestKey1", new ComplexItem()
                    {
                        a = 15
                    }
                }
            },
            ComplexItemExample = new ComplexItem()
            {
                a = 99
            },
            ComplexListExample = new List<ComplexItem>()
            {
                {
                    new ComplexItem()
                    {
                        a = 12
                    }                        
                }
            }
        },
        new Holder()
        {
            Attributes = new Dictionary<string,object>()
            {
                {
                    "TestKey1", new ComplexItem()
                    {
                        a = 10
                    }
                    }
            },
            ComplexItemExample = new ComplexItem()
            {
                a = 12
            },
            ComplexListExample = new List<ComplexItem>()
            {
            {
                new ComplexItem()
                {
                    a = 22
                }                        
                }
            }
        }
    };
    sampleDataGrid.Columns.Add(new DataGridTextColumn() { Binding = new Binding("Attributes[TestKey1].a"), CanUserSort = true, Header = "Dictionary Bound", SortMemberPath = "Attributes[TestKey1].a" });
    sampleDataGrid.Columns.Add(new DataGridTextColumn() { Binding = new Binding("ComplexItemExample.a"), CanUserSort = true, Header = "Item Bound", SortMemberPath = "ComplexItemExample.a" });
    sampleDataGrid.Columns.Add(new DataGridTextColumn() { Binding = new Binding("ComplexListExample[0].a"), CanUserSort = true, Header = "List Bound", SortMemberPath = "ComplexListExample[0].a" });
    sampleDataGrid.ItemsSource = holders;
}

And here are the simplified class definitions.

public class ComplexItem
{
    public int a { get; set; }
    public int b { get; set; }
    public int c { get; set; }
}

public class Holder
{
    public Dictionary<string, object> Attributes { get; set; }
    public List<ComplexItem> ComplexListExample { get; set; }
    public ComplexItem ComplexItemExample { get; set; }
}

And in this example the XAML the following.

<Grid x:Name="LayoutRoot" Background="White">
    <sdk:DataGrid AutoGenerateColumns="False" Name="sampleDataGrid" />
</Grid>

Now the issue arises when I try and sort the column. Nothing happens. I think it has something to do with SortMemberPath not being resolved correctly with the dictionary, but I am at a loss as to solve this. Any help or insight would be greatly appreciated.

Update 07-2开发者_如何学运维8-11:

Just to clarify that the datagrid is bound to holders which is of type List, not a dictionary. The actual rows have their paths set through the dictionary which is resolving without any issue. Not the SortMemberPath is set to the same thing yet to no avail.


Your basic problem is that you're binding to a Dictionary, and there's no way for the DataGrid to sort it because it cann't compare values (IComparable).

You are much better off writing your own custom data objects and then overriding Equals() & GetHasCode() so that the framework can make automatic comparisons of data values.

It's either that, or your other option might be to hook into some kind of event in the datagrid for sorting this behind the scenes in code behind. But really... I mean, at that point you're really fighting the framework and what it does so well, so you might as well just revert to classic ASP.NET.


Well I remembered that this still hadn't been answered and I did discover what was causing the problem. So hopefully this will help someone else out there running into this problem. As it turns out if you explicitly set the dictionary's value type to ComplexItem it will in fact work as expected. When you use the object type it does not actually set the binding (I assume this is to avoid runtime errors). I did not have access to the code where this dictionary was residing and was forced to store the same data in a separate collection as a workaround.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜