WPF 工具包 DataGrid:使用空值排序

发布于 2024-09-30 14:00:55 字数 527 浏览 0 评论 0原文

我想对像这样绑定的列上的 WPF-Toolkit DataGrid 进行排序:

<toolkit:DataGrid.Columns>
    <toolkit:DataGridTextColumn Header="MyColumn" Binding="{Binding AnObject.AProperty}" />
</toolkit:DataGrid.Columns>

现在,当 AnObject 为 null 时,将抛出带有消息“至少一个对象必须实现 IComparable”的 ArgumentException。

解决此限制的最佳方法是什么?我真的不想更改域模型以返回 NullObject-Pattern。

将转换器与直接绑定到 AnObject 结合使用可能会变得相当麻烦,因为我有许多这样的列和多个 DataGrid。

我应该为每个 DataGrid 编写自定义 ViewModel 并在 ItemsSource 属性上使用转换器吗?

我很感激每一个建议!

I want to sort a WPF-Toolkit DataGrid on a column which is bound like this:

<toolkit:DataGrid.Columns>
    <toolkit:DataGridTextColumn Header="MyColumn" Binding="{Binding AnObject.AProperty}" />
</toolkit:DataGrid.Columns>

Now when AnObject is null, an ArgumentException with the Message "At least one object must implement IComparable" will be thrown.

What's the best way to work around this limitation? I don't really want to change the domain model to return a NullObject-Pattern.

Using Convertors in combination with a binding directly to AnObject could turn out to be quite cumbersome, since I have many of those columns and more than one DataGrid.

Should I write a custom ViewModel foreach DataGrid and use a Converter on the ItemsSource Property?

I appreciate every suggestion!

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

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

发布评论

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

评论(2

甜扑 2024-10-07 14:00:55

我会采用不同的策略:为 AnObject 创建一个 ViewModel,其中包含一个公开 AProperty 的属性,并在所有 DataGrid 上使用它。当 AnObject 为 null 时,VM 应该能够在 AProperty 上发送适当的值。

I'd go with a different strategy: create a ViewModel for AnObject with a property that exposes AProperty and use it on all DataGrids. The VM should be capable of sending out the appropriate values on AProperty when AnObject is null.

谢绝鈎搭 2024-10-07 14:00:55

在这里找到了最好的解决方案:

WPF Datagrid 对包含 null 元素的列进行排序

为我的 DataGrid 实现了一个自定义排序器,用于转换空值。

Found the best solution here:

WPF Datagrid sort on column with null elements

Implemented a custom sorter for my DataGrids which convers null values.

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