WPF DataGrid中(在.NET 4.0中没有工具包),部首排序用的ICollectionView [英] WPF DataGrid (In .NET 4.0 Not Toolkit), Header Sorting with ICollectionView
问题描述
首先让我澄清(如果标题是不够清楚):我讨论 System.Windows.Controls.DataGrid
从.NET 4.0框架的不可以工具包的版本。的
我目前正在建立一个小型可重复使用的组类/视图模型/等我的项目,以建立一个相当强大的DataGrid我的应用程序。
现在,在默认情况下给予 IEnumarable<>
的ItemsSource的DataGrid控件支持标题单击列排序
不过,我实现现在使用 CollectionViewSource
和的ICollectionView
公开数据成员到DataGrid。当以这种方式挂接似乎依赖SortDescriptions排序
虽然我喜欢有从code控制(可挂接到各种各样的东西),我还需要的能力为我的用户点击标题栏来thier结果进行排序。我正在寻找一种方式来勾头栏点击信号我的code键调节 CollectionViewSource
适当。
- 难道我将不得不重新风格的标题按钮做火的事件?
- 有没有办法挂钩从DataGrid控件的现有标题排序事件?
- 什么是去了解这一点的最好方法是什么?
- 在我使用
的ICollectionView
不正确?
其实我找到了答案。对我而言一个完整的错误。它源于对列在DataGrid控件设置。
必须确保该 CanUserSort =真
设置在的DataGridColumn
,而在 SortMemberPath
属性设置。
例如:
< DataGridTemplateColumn标题=帐号WIDTH =自动CanUserSort =真SortMemberPath =AccountNum>
< DataGridTemplateColumn.CellTemplate>
<的DataTemplate>
< TextBlock的文本={结合AccountNum}/>
< / DataTemplate中>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>
Firstly let me clarify (if the title wasn't clear enough): I'm discussing System.Windows.Controls.DataGrid
from the .NET 4.0 Framework, not the toolkit edition.
I'm currently building up a small re-usable set of classes/view models/etc in my project to build a rather powerfull DataGrid for my application.
Now, by default given an IEnumarable<>
ItemsSource the DataGrid control supports header-clicking column sorting.
However, my implementation now uses a CollectionViewSource
and ICollectionView
to expose data members to the DataGrid. When hooked up in this fashion it seems to rely on the SortDescriptions to sort.
While I like having control from code (could be hooked into all sorts of things) I also require the ability for my users to click the header bars to sort thier results. I'm looking for a way to hook the header-bar clicks to signal my code to adjust the CollectionViewSource
appropriately.
- Am I going to have to re-style the header buttons to do fire the events?
- Is there a way to hook the sort events from the DataGrid control's existing headers?
- What is the best way to go about this?
- Am I using
ICollectionView
incorrectly?
Actually I found the answer. A complete blunder on my part. It stemmed from settings on the Columns in the DataGrid Control.
Had to make sure that CanUserSort="True"
was set on the DataGridColumn
, and that the SortMemberPath
property was set.
Example:
<DataGridTemplateColumn Header="Account #" Width="Auto" CanUserSort="True" SortMemberPath="AccountNum">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding AccountNum}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
这篇关于WPF DataGrid中(在.NET 4.0中没有工具包),部首排序用的ICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!