WPF Datagrid对带有空元素的列进行排序 [英] WPF Datagrid sort on column with null elements

查看:336
本文介绍了WPF Datagrid对带有空元素的列进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF Datagrid,我正在使用许多列。其中一列具有一些有时为空的元素,当我在此列上尝试排序时会导致异常。



列的定义如下: / p>

 < dg:DataGrid.Columns> 
< dg:DataGridTextColumn Binding ={Binding MyObject.Field1}Header =Field1Width =Auto/>
< dg:DataGridTextColumn Binding ={Binding MyObject.Field2.SubField}Header =Field2Width =Auto/>
< / dg:DataGrid.Columns>

如果我对Field1列进行排序,那么如果我对Field2列进行排序,并且没有null Field2对象很好,但有时候有DataGrid尝试在SubField上进行排序(我猜),并遇到一个空异常:

  System.InvalidOperationException未处理
消息=添加的SortDescriptions无效。可能的解决方案是将列上的CanUserSort设置为false,或者在Column上使用SortMemberPath属性,或者处理DataGrid上的Sorting事件。

我已经尝试将SortMemberPath设置为MyObject.Field2.SubField,但当然这不会修复因为Field2仍然有时为空。我想知道尝试使用一个转换器,我设置了SortMemberPath,并且该转换器为任何null元素返回string.empty,但是无法使其工作。



我还尝试在这些列的绑定中添加TargetNullValue = {x:Static sys:String.Empty},但仍然没有起作用。



任何建议或建议将不胜感激。
谢谢,

解决方案

一般建议是:不要使用SortMemberPath。不仅仅是因为你刚刚遇到的问题。但也是因为它们是超级的。



而是使用ListCollectionView类的CustomSort属性。有关详细信息,请参阅改进Microsoft DataGrid CTP排序性能改进Microsoft DataGrid CTP排序性能 - 第2部分。虽然它表示有意义的表现,但它也显示了如何解决您的问题。



希望这有助于:)。


I have a WPF Datagrid that I am using with a number of columns. One of the columns has some elements that are sometimes null and this causes an exception when I try and sort on this column.

The definitions of the columns is something like:

<dg:DataGrid.Columns>
  <dg:DataGridTextColumn Binding="{Binding MyObject.Field1}" Header="Field1" Width="Auto" />
  <dg:DataGridTextColumn Binding="{Binding MyObject.Field2.SubField}" Header="Field2" Width="Auto" />
</dg:DataGrid.Columns>

If I sort on Field1 column it is fine, if I sort on Field2 column and there are no null Field2 objects it is fine, but sometimes there are and the DataGrid tries to sort on the SubField (I guess) and hits a null exception:

System.InvalidOperationException was unhandled
  Message=The SortDescriptions added are not valid. The probable solutions are to set the CanUserSort on the Column to false, or to use SortMemberPath property on the Column, or to handle the Sorting event on DataGrid.

I have tried setting SortMemberPath to "MyObject.Field2.SubField" but of course this doesn't fix it since Field2 is still sometimes null. I wondered about trying to use a converter where I set the SortMemberPath and have that converter return string.empty for any null elements but couldn't get it to work.

I also tried adding "TargetNullValue={x:Static sys:String.Empty}" within the binding of these columns but it still did not work.

Any advice/suggestions would be most appreciated. Thanks, Will

解决方案

General advice would be: Don't use SortMemberPath. Not only because of the problem you've just met. But also because they are ultraslow.

Instead use CustomSort property of the ListCollectionView class. For more details read "Improving Microsoft DataGrid CTP sorting performance" and "Improving Microsoft DataGrid CTP sorting performance - Part 2". Although it says "imporve performance" it also shows how to fix your problem.

Hope this helps :).

这篇关于WPF Datagrid对带有空元素的列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆