gpt4 book ai didi

c# - 每个列的 WPF DataGrid CustomSort

转载 作者:可可西里 更新时间:2023-11-01 07:50:09 26 4
gpt4 key购买 nike

我有一个绑定(bind)到封装 ObservableCollection 的 CollectionViewSource 的 WPF DataGrid。这个 CollectionViewSource 有两个主要目标:

1) 按 T 的特定属性对每个项目进行分组。我在 GroupDescription 中使用 ValueConverter 来获得我想要的分组行为。

2) 按 a) 主要是组名称(如上定义)和 b) 各个组项目对网格进行排序。我通过将自定义 IComparer 附加到 CollectionViewSource 的“CustomSort”属性来实现这一点。

这在大多数情况下效果很好,但是一旦单击列标题,排序逻辑就会被覆盖。我不想禁用排序,但我想知道是否可以为特定列分配自定义排序顺序?

为了让事情更清楚一点,假设用户单击“ColumnA”——此刻,我的 CustomSorter 封装的排序逻辑被覆盖,DataGrid 现在按该属性排序。我不想按所选属性排序,而是想反转 CustomSorter 的逻辑。

最佳答案

我创建了几个附加属性来处理这个问题。我希望这对某人有用!

首先 - 一个用于定向比较器的简单界面。这扩展了 IComparer,但又给了我们一个属性 (SortDirection)。您的实现应该使用它来确定元素的正确顺序(否则会丢失)。

public interface ICustomSorter : IComparer
{
ListSortDirection SortDirection { get; set; }
}

接下来是附加行为 - 它做了两件事:1) 告诉网格使用自定义排序逻辑 (AllowCustomSort=true) 和 b) 让我们能够在每列级别设置此逻辑。

public class CustomSortBehaviour
{
public static readonly DependencyProperty CustomSorterProperty =
DependencyProperty.RegisterAttached("CustomSorter", typeof(ICustomSorter), typeof(CustomSortBehaviour));

public static ICustomSorter GetCustomSorter(DataGridColumn gridColumn)
{
return (ICustomSorter)gridColumn.GetValue(CustomSorterProperty);
}

public static void SetCustomSorter(DataGridColumn gridColumn, ICustomSorter value)
{
gridColumn.SetValue(CustomSorterProperty, value);
}

public static readonly DependencyProperty AllowCustomSortProperty =
DependencyProperty.RegisterAttached("AllowCustomSort", typeof(bool),
typeof(CustomSortBehaviour), new UIPropertyMetadata(false, OnAllowCustomSortChanged));

public static bool GetAllowCustomSort(DataGrid grid)
{
return (bool)grid.GetValue(AllowCustomSortProperty);
}

public static void SetAllowCustomSort(DataGrid grid, bool value)
{
grid.SetValue(AllowCustomSortProperty, value);
}

private static void OnAllowCustomSortChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var existing = d as DataGrid;
if (existing == null) return;

var oldAllow = (bool)e.OldValue;
var newAllow = (bool)e.NewValue;

if (!oldAllow && newAllow)
{
existing.Sorting += HandleCustomSorting;
}
else
{
existing.Sorting -= HandleCustomSorting;
}
}

private static void HandleCustomSorting(object sender, DataGridSortingEventArgs e)
{
var dataGrid = sender as DataGrid;
if (dataGrid == null || !GetAllowCustomSort(dataGrid)) return;

var listColView = dataGrid.ItemsSource as ListCollectionView;
if (listColView == null)
throw new Exception("The DataGrid's ItemsSource property must be of type, ListCollectionView");

// Sanity check
var sorter = GetCustomSorter(e.Column);
if (sorter == null) return;

// The guts.
e.Handled = true;

var direction = (e.Column.SortDirection != ListSortDirection.Ascending)
? ListSortDirection.Ascending
: ListSortDirection.Descending;

e.Column.SortDirection = sorter.SortDirection = direction;
listColView.CustomSort = sorter;
}
}

要使用它,请在您的 XAML 中实现一个 ICustomComparer(带有无参数构造函数):

<UserControl.Resources>
<converters:MyComparer x:Key="MyComparer"/>
<!-- add more if you need them -->
</UserControl.Resources>
<DataGrid behaviours:CustomSortBehaviour.AllowCustomSort="True" ItemsSource="{Binding MyListCollectionView}">
<DataGrid.Columns>
<DataGridTextColumn Header="Test" Binding="{Binding MyValue}" behaviours:CustomSortBehaviour.CustomSorter="{StaticResource MyComparer}" />
</DataGrid.Columns>
</DataGrid>

关于c# - 每个列的 WPF DataGrid CustomSort,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18122751/

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