using System.ComponentModel; using System.Globalization; using System.Windows; using System.Windows.Controls; using System.Windows.Data; namespace Plugin.DataViewer { public class CustomSortColumnConverter : IValueConverter { public static CustomSortColumnConverter Instance = new CustomSortColumnConverter(); public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value as ListSortDirection? == ListSortDirection.Ascending) { return true; } if (value as ListSortDirection? == ListSortDirection.Descending) { return false; } return null; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return value; } } public interface ICustomSortColumn { public bool? IsAsc { get; set; } void UpdateOther(bool? value); DataGrid GetOwner(); } public class CustomSortTemplateColumn : DataGridTemplateColumn, ICustomSortColumn { public static DependencyProperty IsAscProperty = DependencyProperty.Register( "IsAsc", typeof(bool?), typeof(DataGridTemplateColumn), new FrameworkPropertyMetadata(null)); public bool? IsAsc { get => (bool?)GetValue(IsAscProperty); set { SetValue(IsAscProperty, value); } } public void UpdateOther(bool? value) { foreach (var col in DataGridOwner.Columns) { if (col == this) continue; if (col is ICustomSortColumn csc) { csc.IsAsc = value; } } } public DataGrid GetOwner() { return this.DataGridOwner; } } public class CustomSortTextColumn : DataGridTextColumn, ICustomSortColumn { public static DependencyProperty IsAscProperty = DependencyProperty.Register( "IsAsc", typeof(bool?), typeof(DataGridTextColumn), new FrameworkPropertyMetadata(null)); public bool? IsAsc { get => (bool?)GetValue(IsAscProperty); set { SetValue(IsAscProperty, value); } } public void UpdateOther(bool? value) { foreach (var col in DataGridOwner.Columns) { if (col == this) continue; if (col is ICustomSortColumn csc) { csc.IsAsc = value; } } } public DataGrid GetOwner() { return this.DataGridOwner; } } }