gpt4 book ai didi

c# - 如何禁用 Datagrid 中的排序组顺序,但保持组内列的排序?

转载 作者:太空宇宙 更新时间:2023-11-03 15:40:35 25 4
gpt4 key购买 nike

我设置了一个 DataGrid 并将其绑定(bind)到一个 ICollectionView,我向其中添加了一个组。

myView.Add(new PropertyGroupDescription("MyProp"));

然后添加一个简单的组样式:

                <DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Padding="3"/>
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text="{Binding Path=ItemCount}" Margin="8,0,4,0"/>
<TextBlock Text="Element(s)"/>
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>

无论如何,组本身最初是按照我希望的顺序排列的。但是,当用户单击 DataGrid 中的标题以按该列排序时,它不仅对列,但它也按名称顺序对组进行排序。因此,如果我的组按 B、A、C 的顺序排列,它们将被排序为 A、B、C,然后对每个组中的列进行排序。

是否有一个简单的设置可以让列排序只对列排序而不对组排序。

需要明确的是,我不是在问如何禁用排序,而是在问如何在不按名称对组本身重新排序的情况下对组内的列进行排序。

最佳答案

我怀疑是否有任何简单的设置可以在组保持相同顺序时对数据进行排序,但是您可以定义自定义排序器并处理 DataGrid.Sorting 事件以根据所需顺序对数据进行排序。

假设 Category 是分组所基于的属性,我们有这样的东西:

MyList.Add(new ItemViewModel { ID = 1, Amount = 5, Category = "A" , ...});
// add some more items

GroupedList = new ListCollectionView(MyList);
GroupedList.GroupDescriptions.Add(new PropertyGroupDescription("Category"));

然后你需要编写一个自定义排序器,根据你想要的组顺序作为主要排序参数对数据进行排序(以保持组顺序不变),然后按用户选择的列作为次要参数对数据进行排序(以对每个组内的数据进行排序) .

请注意,我写这篇文章时假设 Category 只能接受“A”、“B”和“C”,而且您想要的团体订单是 B-A-C,就像您提到的那样。

public class CustomSorter : IComparer
{
public ListSortDirection? Direction { get; set; }
public string SortMember { get; set; }

public CustomSorter(string sortMember, ListSortDirection? direction)
{
SortMember = sortMember;
Direction = direction;
}

// A custom comparer based on group header ("Category" in this example)
// in "B"<"A"<"C" order.
public int Compare(object x, object y)
{
var xVm = (ItemViewModel)x;
var yVm = (ItemViewModel)y;

if (xVm.Category == yVm.Category)
return CompareBySortMember(xVm,yVm);
if (xVm.Category == "B")
return -1;
if (xVm.Category == "A" && yVm.Category == "C")
return -1;
return 1;
}

// When two items have the same Category, comparison is made based on
// "SortMemberPath" property of the sorting column.
private int CompareBySortMember(ItemViewModel xVm, ItemViewModel yVm)
{
var xValue = xVm.GetType().GetProperty(SortMember).GetValue(xVm);
var yValue = yVm.GetType().GetProperty(SortMember).GetValue(yVm);
int result = xValue.ToString().CompareTo(yValue.ToString());
return Direction == ListSortDirection.Ascending ? result * -1 : result;
}
}

最后:

    private void DataGrid_Sorting(object sender, DataGridSortingEventArgs e)
{
e.Handled = true;

// Set the sort order on the column
e.Column.SortDirection = (e.Column.SortDirection != ListSortDirection.Ascending)
? ListSortDirection.Ascending : ListSortDirection.Descending;

// Sort data based on predefined order of group headers and currently sorting column.
GroupedList.CustomSort = new CustomSorter(e.Column.SortMemberPath, e.Column.SortDirection);
}

关于c# - 如何禁用 Datagrid 中的排序组顺序,但保持组内列的排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30403525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com