对 WPF DataGrid 进行排序,MVVM 风格

发布于 2024-08-14 12:32:54 字数 1266 浏览 2 评论 0原文

我正在尝试使用 WPF 工具包 DataGrid 进行排序。我的 DataGrid 的行是视图模型的实例。行的视图模型公开每列的视图模型。每列都是针对不同用户控件的数据模板。这就是我的 DataGrid 的列声明的样子:

<tk:DataGrid.Columns>
    <tk:DataGridTemplateColumn Header="Name" MinWidth="150" Width="150">
        <tk:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <ContentPresenter Content="{Binding Path=NameViewModel}"/>
            </DataTemplate>
        </tk:DataGridTemplateColumn.CellTemplate>
    </tk:DataGridTemplateColumn>
    <tk:DataGridTemplateColumn Header="Data Dependencies" MinWidth="350" Width="Auto">
        <tk:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <ContentPresenter Content="{Binding Path=DependenciesViewModel}"/>
            </DataTemplate>
        </tk:DataGridTemplateColumn.CellTemplate>
    </tk:DataGridTemplateColumn>
</tk:DataGrid.Columns>

Name 绑定到视图模型,该视图模型是使用将名称显示为文本块的用户控件进行模板化的数据。它还显示一些其他图形信息,这就是它显示在用户控件中的原因。

这样做的问题是我失去了对 Name 列进行排序的能力。我希望在公开名称视图模型的行视图模型上实现 IComparable 能够解决问题,但 WPF 数据网格看起来并不关心它实现 IComparable

有人对如何最好地解决这个问题有任何建议吗?

I'm trying to get sorting to work with a WPF Toolkit DataGrid. My DataGrid's rows are instances of view models. The row's view model exposes a view model for each column. Each column is data templated to a different user control. This is what my DataGrid's column declarations look like:

<tk:DataGrid.Columns>
    <tk:DataGridTemplateColumn Header="Name" MinWidth="150" Width="150">
        <tk:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <ContentPresenter Content="{Binding Path=NameViewModel}"/>
            </DataTemplate>
        </tk:DataGridTemplateColumn.CellTemplate>
    </tk:DataGridTemplateColumn>
    <tk:DataGridTemplateColumn Header="Data Dependencies" MinWidth="350" Width="Auto">
        <tk:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <ContentPresenter Content="{Binding Path=DependenciesViewModel}"/>
            </DataTemplate>
        </tk:DataGridTemplateColumn.CellTemplate>
    </tk:DataGridTemplateColumn>
</tk:DataGrid.Columns>

Name is bound to a view model that is data templated with a user control that displays the name as a text block. It also displays some other graphical information, which is why it's displayed in a user control.

The problem with doing it this way is that I lose the ability to sort the Name column. I was hoping that implementing IComparable<T> on the row view model that exposes the Name view model would do the trick, but it doesn't look like the WPF data grid cares that it implements IComparable<T>.

Does anyone have any suggestions as to how best to tackle this?

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

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

发布评论

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

评论(1

哥,最终变帅啦 2024-08-21 12:32:54

这个答案是什么我正在寻找。

This answer was what I was looking for.

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