wenxiangchao
4 天以前 2e889843b4c171f2d6ad8e0d53084040703d11f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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;
        }
    }
}