按顺序分组?
在WPF中,我可以分组,但默认是按升序分组。 我的需要之一是能够控制组的排序(升序或降序)。 例如:
group 1
- item 1.1
- item 1.2
- item 1.3
group 2
- item 2.1
- item 2.2
也可以切换到:
group 2
- item 2.1
- item 2.2
group 1
- item 1.1
- item 1.2
item 1.3
//这里是为特定列设置组的函数: 私有无效SetupGrouping(DataGridparentGrid,DataGridColumn col) { if (parentGrid == null || col == null || string.IsNullOrEmpty(col.SortMemberPath)) 返回; ICollectionView vw = GetDefaultView(); if (vw != null && vw.CanGroup) { if (vw.GroupDescriptions.Count != 0) { vw.GroupDescriptions.Clear(); } PropertyGroupDescription gd = new PropertyGroupDescription(col.SortMemberPath); // 检查该列是否为优先级,如果是 // 然后在顶部进行高优先级 (3) 的分组。 // 顺序应该是高(3)、正常(2)、低(1) DataGridColumn priCol = GetColumnByID(ColumnFlags.Priority); if(col == priCol) { // 尝试通过添加组来更改添加的排序方向。 // 但是,它抱怨 SortDescription 被密封时出错 // 并且无法更改。 //if (vw.SortDescriptions != null && vw.SortDescriptions.Count > 0) //{ // 排序描述 sd = vw.SortDescriptions[0]; // if (sd.PropertyName == col.SortMemberPath) // { // sd.Direction = ListSortDirection.Descending; // } //} } // 信息:当我们将新的 GroupDescription 添加到 GroupDescriptions 列表时, // 客人什么? 一个新的 SortDescription 也被添加到 // 排序描述列表。 vw.GroupDescriptions.Add(gd); } // 保存该列以供以后使用 分组列 = col; // 设置DataGrid的Tag,以便GroupSyle可以获取列名 ParentGrid.Tag = DispatchAttachedProperties.GetColumnHeader(col); }
In WPF, I can group, but the default is group with ascending. One of my need to to be able to control the sorting of the group (Ascending or descending). For example:
group 1
- item 1.1
- item 1.2
- item 1.3
group 2
- item 2.1
- item 2.2
and also be able to switch to:
group 2
- item 2.1
- item 2.2
group 1
- item 1.1
- item 1.2
item 1.3
//Here is the function to setup a group for a particular column: private void SetupGrouping(DataGrid parentGrid, DataGridColumn col) { if (parentGrid == null || col == null || string.IsNullOrEmpty(col.SortMemberPath)) return; ICollectionView vw = GetDefaultView(); if (vw != null && vw.CanGroup) { if (vw.GroupDescriptions.Count != 0) { vw.GroupDescriptions.Clear(); } PropertyGroupDescription gd = new PropertyGroupDescription(col.SortMemberPath); // Check to see if the column is Priority, if it is // then do the grouping with high priority (3) on top. // The order should be High(3), Normal (2), Low(1) DataGridColumn priCol = GetColumnByID(ColumnFlags.Priority); if(col == priCol) { // Attempted to change the direction of the sort added by adding group. // However, it has error complaining SortDescription is sealed // and can't be changed. //if (vw.SortDescriptions != null && vw.SortDescriptions.Count > 0) //{ // SortDescription sd = vw.SortDescriptions[0]; // if (sd.PropertyName == col.SortMemberPath) // { // sd.Direction = ListSortDirection.Descending; // } //} } // Info: when we add a new GroupDescription to GroupDescriptions list, // guest what? a new SortDescription is also added to the // SortDescriptions list. vw.GroupDescriptions.Add(gd); } // Save off the column for later use GroupedColumn = col; // Set the DataGrid's Tag so that the GroupSyle can get the column name parentGrid.Tag = DispatchAttachedProperties.GetColumnHeader(col); }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的想法是正确的。 根据您要分组的同一属性,将 SortDescription 添加到 ICollectionView。 如果要更改排序方向,则必须清除现有的排序方向并添加新的相反方向。 正如您发现的那样,一旦创建它,您就无法更改它。
You had the right idea. Add a SortDescription to the ICollectionView based on the same property you are grouping on. If you want to change sort directions, you have to clear the existing one and add a new for the opposite direction. You can't change it once its created as you discovered.