2

I have an interface

public interface IProperty
{
    string Name { get; }
}

and an explicit implementation of it

public class Parameter : IProperty
{
    private readonly string m_name;

    public Parameter(string name) { m_name = name; }

    string IProperty.Name { get { return m_name; } }
}

I have a DataGrid that is showing an ObservableCollection<IProperty>. The only column, namely DataGridTextColumn is sorting the rows by virtue of the property SortMemberPath="(this:IProperty.Name)" (I got the idea of binding to explicit member implementations from this forum thread).

So the problem is: How to have default rows sorting? I tried to do this in the window constructor:

var sortDescription = new SortDescription("(this:IProperty.Name)", ListSortDirection.Ascending);
m_dataGrid.Items.SortDescriptions.Add(sortDescription);

with almost no luck. The effect is:

  • The rows are sorted in some unspecified order
  • I get lots of errors in the Visual Studio Output window:
    System.Windows.Data Error: 39 : BindingExpression path error: '(this:IProperty.Name)' property not found on 'object' ''Parameter' (HashCode=25584554)'. null
  • And the most interesting: when I apply any filtering (CollectionView.Filter) after collection is viewed - rows magically start being sorted correctly!

Does anybody have an idea why the rows are not being sorted correctly from the very beginning?
If it matters I'm targeting .NET Framework v3.5

alpha-mouse
  • 4,953
  • 24
  • 36
  • I don't understand. You have a grid showing one `IProperty` per row, and each `IProperty` has a `Name` property by which you want to sort that column? That makes no sense so I'm assuming I misunderstood you. Posting all your code would help. – Kent Boogaart Nov 03 '10 at 19:43
  • I simplified the code to the minimum that still causes this strange behavior. Your idea is correct, one IProperty per row and only one column in the table. – alpha-mouse Nov 04 '10 at 09:14
  • Was a solution ever found to this issue? I'm having the same problem now, as a result of using PostSharp to dynamically introduce interfaces to my model objects which form the DataGrid ItemsSource collection. Same issue with adding GroupDescriptions to explicit interface properties also, and as for you - once the Window is up, I can sort and everything groups correctly too. Gah! – RJ Lohan Nov 30 '12 at 03:15
  • @RJ Lohan: I barely remember this issue, but I suppose that I didn't find any solution:( Otherwise I would have posted it here. Sorry. – alpha-mouse Nov 30 '12 at 10:26

1 Answers1

1

You should use ListCollectionView as the CollectionView of your DataGrid. Then configure the sorting logic through ListCollectionView.CustomSort. See an example here.

codeDom
  • 1,623
  • 18
  • 54