This is a migrated thread and some comments may be shown as answers.

How to sort an ObservableCollection containing mixed types?

2 Answers 634 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Marc
Top achievements
Rank 1
Marc asked on 27 Oct 2011, 02:31 PM
Hi,

In our project we are using the RadGridView to display the contents of a conceptual folder: the contents of a folder can be either other folders / and / or items.
The grid contains an ObservableCollection contains objects of two types: "Folder" and "Item", which have no common base type, but both have the property called "Name" returning a string.
When the grid shows listitems which are of the same type, eg. only folders, or only items, sorting by the "Name" column (bound to the name property), works just fine.

Our problem:
When the grid shows listitems which are of the mixed types, eg. folders and items, sorting by the "Name" column (bound to the name property), only sorts the first time, but from there on does nothing.

The mentioned column is defined within the RadGridView as follows:

<telGrid:GridViewDataColumn Width="300" Header="Name" DataMemberBinding="{Binding Name}" CellStyleSelector="{StaticResource ColumnNameCellStyleSelector}">

the collection holding both types of folder and items is defined on our model as follows:

ObservableCollection<object> _items = new ObservableCollection<object>();

We are leaning towards implementing custom sorting, but it feels like a workaround to do such, so our question is:
What would be your recommended approach to support sorting of mixed types in the Observable collection?

Further details:

WPF 4.5
Windows 7 64x
Telerik RadControls for WPF v2011.2.1004.40 / Runtime v4.0.30319
Language C#

Awaiting your response and kind regards.

2 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 28 Oct 2011, 01:54 PM
Hello Marc,

The best approach would be to use an Interface as a base for the two different types. Your two types should implement tis interface. Then bind to an ObservableCollection of the interface(ObservableCollection<ITestItem>).

public class TestItem1 : ITestItem
    {
        public int Prop1 { get; set; }
 
        private static Random rnd = new Random();
 
        public TestItem1()
        {
            Prop1 = rnd.Next(100);;
        }
    }
 
    public class TestItem2:ITestItem
    {
        public int Prop1 { get; set; }
 
        private static Random rnd = new Random();
 
        public TestItem2()
        {
            Prop1 = rnd.Next(100);
        }
    }
 
    public interface ITestItem
    {
        int Prop1 { get; set; }
    }
}

I hope that this is helpful. 

Greetings,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Marc
Top achievements
Rank 1
answered on 09 Nov 2011, 01:37 PM
Thank you for your response, works.
Tags
GridView
Asked by
Marc
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Marc
Top achievements
Rank 1
Share this question
or