SortDescription 绑定到显式接口实现

发布于 2024-09-30 04:06:30 字数 1279 浏览 0 评论 0原文

我有一个接口

public interface IProperty
{
    string Name { get; }
}

及其显式实现

public class Parameter : IProperty
{
    private readonly string m_name;

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

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

我有一个显示 ObservableCollectionDataGrid。唯一的列,即 DataGridTextColumn 是凭借属性 SortMemberPath="(this:IProperty.Name)" 对行进行排序(我想到了绑定到显式成员此论坛帖子)。

所以问题是:如何进行默认行排序?我尝试在窗口构造函数中执行此操作:

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

几乎没有运气。效果是:

  • 行按某种未指定的顺序排序
  • 我在 Visual Studio 输出窗口中收到很多错误:
    System.Windows.Data 错误:39:BindingExpression 路径错误:在“对象”“参数”(HashCode=25584554) 上找不到“(this:IProperty.Name)”属性。 null
  • 最有趣的是:当我在查看集合后应用任何过滤(CollectionView.Filter)时 - 行神奇地开始正确排序!

有人知道为什么行从一开始就没有正确排序吗?
如果重要的话我的目标是 .NET Framework v3.5

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七堇年 2024-10-07 04:06:30

您应该使用 ListCollectionView 作为 DataGrid 的 CollectionView。然后通过 ListCollectionView.CustomSort。请参阅此处的示例。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文