SortDescription 绑定到显式接口实现
我有一个接口
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; } }
}
我有一个显示 ObservableCollection
所以问题是:如何进行默认行排序?我尝试在窗口构造函数中执行此操作:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用
ListCollectionView
作为 DataGrid 的CollectionView
。然后通过 ListCollectionView.CustomSort。请参阅此处的示例。You should use
ListCollectionView
as theCollectionView
of your DataGrid. Then configure the sorting logic through ListCollectionView.CustomSort. See an example here.