开发者

SortDescription bound to an explicit interface implementation

I have an interf开发者_Go百科ace

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


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜