对 WPF DataGrid 进行排序,MVVM 风格
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个答案是什么我正在寻找。
This answer was what I was looking for.