WPF数据网格按所选列自动排序

时间:2015-12-22 17:52:52

标签: c# wpf xaml datagrid

目前我正在研究WPF的数据网格。我遇到了自动排序的问题。这是我的xaml:

<DataGrid x:Name="customTasksDataGrid" Margin="10,10,10,38" Grid.Column="1" IsReadOnly="True" AutoGenerateColumns="False">
<DataGrid.Columns>
    <DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
    <DataGridTextColumn Header="Client" Binding="{Binding Client.Names}"/>
    ...
    <DataGridTextColumn Header="DueDate" Binding="{Binding DueDate, StringFormat=\{0:dd.MM.yy HH:mm\}}" SortDirection="Ascending">
        <DataGridTextColumn.ElementStyle>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="Foreground" Value="{Binding Path=., Converter={StaticResource converter}}"/>
            </Style>
        </DataGridTextColumn.ElementStyle>
    </DataGridTextColumn>
    ...
</DataGrid.Columns>

问题是:当我更新我的datagrid的ItemsSource时,datagrid按ID列排序,而不是DueDate排序。当对我的数据网格执行任何更新操作时,如何通过DueDate实现自动排序?

我尝试使用来自[ImplementPropertyChanged]的{​​{1}}标记并将其放在我的PropertyChanged.Fody课程之前,但这种方法根本不起作用(甚至不知道是否需要)

编辑:

可以通过以下方式完成:

CustomTask

2 个答案:

答案 0 :(得分:2)

您可以在Code Behind中对数据网格进行简单排序:

datagrid.Items.SortDescriptions.Add(new SortDescription("DueDate", ListSortDirection.Ascending));

答案 1 :(得分:0)

或使用 xaml 扩展:

<UserControl xmlns:e="clr-namespace:Xaml.Extensions">
    <DataGrid e:DataGridExtensions.DefaultSorting="A:Date">
    <DataGrid e:DataGridExtensions.DefaultSorting="D:Time">
</UserControl>
using System;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;

#nullable enable

namespace Xaml.Extensions
{
    public static class DataGridExtensions
    {
        #region DefaultSorting

        public static readonly DependencyProperty DefaultSortingProperty =
            DependencyProperty.RegisterAttached(
                nameof(DefaultSortingProperty).Replace("Property", string.Empty),
                typeof(string),
                typeof(DataGridExtensions),
                new PropertyMetadata(string.Empty, OnDefaultSortingChanged));

        public static string? GetDefaultSorting(DependencyObject element)
        {
            return (string?)element.GetValue(DefaultSortingProperty);
        }

        public static void SetDefaultSorting(DependencyObject element, string? value)
        {
            element.SetValue(DefaultSortingProperty, value);
        }

        private static void OnDefaultSortingChanged(
            DependencyObject element,
            DependencyPropertyChangedEventArgs args)
        {
            if (element is not DataGrid dataGrid)
            {
                throw new ArgumentException("Element should be DataGrid.");
            }
            if (args.NewValue is not string sorting)
            {
                throw new ArgumentException("Type should be string.");
            }

            var values = sorting.Split(':');
            if (values.Length != 2)
            {
                throw new InvalidOperationException("String should be like 'A:Name' or 'D:Name'.");
            }

            dataGrid.Items.SortDescriptions.Clear();
            dataGrid.Items.SortDescriptions.Add(
                new SortDescription(
                    values[1],
                    values[0] == "D"
                        ? ListSortDirection.Descending
                        : ListSortDirection.Ascending));
        }

        #endregion
    }
}

如果您指定 DesignInstance,这也可以在设计器中实时运行。