-
Notifications
You must be signed in to change notification settings - Fork 119
/
ChoGridViewColumnVisibilityManager.cs
183 lines (160 loc) · 6.06 KB
/
ChoGridViewColumnVisibilityManager.cs
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace ChoEazyCopy
{
public class ChoGridViewColumnVisibilityManager
{
private static Dictionary<GridView, Dictionary<GridViewColumn, double>> _columns = null;
public static void ResizeAllColumnsToFit(DependencyObject obj)
{
ListView lv = obj as ListView;
if (lv == null) return;
GridView gridview = lv.View as GridView;
if (gridview == null || gridview.Columns == null) return;
var columns = _columns;
if (columns == null || !_columns.ContainsKey(gridview))
return;
if (_columns[gridview] == null)
return;
foreach (GridViewColumn gc in _columns[gridview].Keys.ToArray())
{
gc.Width = 0;
gc.Width = Double.NaN;
columns[gridview][gc] = gc.Width;
}
}
public static bool Contains(GridViewColumn col)
{
Dictionary<GridViewColumn, double> dict = null;
return Contains(col, out dict);
}
public static bool Contains(GridViewColumn col, out Dictionary<GridViewColumn, double> dict)
{
dict = null;
if (col == null)
return false;
if (_columns == null)
return false;
foreach (var gv in _columns.Keys.ToArray())
{
if (_columns[gv].ContainsKey(col))
{
dict = _columns[gv];
return true;
}
}
return false;
}
public static void SetGridColumnWidth(GridViewColumnHeader colHeader)
{
SetGridColumnWidth(colHeader.Column);
}
public static void SetGridColumnWidth(GridViewColumn col)
{
Dictionary<GridViewColumn, double> dict = null;
if (Contains(col, out dict) && (col.Width > 0 || col.Width == double.NaN))
dict[col] = col.Width;
}
static void UpdateListView(ListView lv)
{
GridView gridview = lv.View as GridView;
if (gridview == null || gridview.Columns == null) return;
if (_columns == null)
_columns = new Dictionary<GridView, Dictionary<GridViewColumn, double>>();
if (!_columns.ContainsKey(gridview))
{
_columns.Add(gridview, new Dictionary<GridViewColumn, double>());
foreach (GridViewColumn gc in gridview.Columns)
_columns[gridview].Add(gc, gc.Width);
}
List<GridViewColumn> toRemove = new List<GridViewColumn>();
foreach (GridViewColumn gc in gridview.Columns)
{
if (!GetIsVisible(gc))
{
gc.Width = 0;
((GridViewColumnHeader)gc.Header).IsHitTestVisible = false;
}
else
{
Dictionary<GridViewColumn, double> dict = null;
if (Contains(gc, out dict))
{
gc.Width = dict[gc];
((GridViewColumnHeader)gc.Header).IsHitTestVisible = true;
}
}
}
}
public static bool GetIsVisible(DependencyObject obj)
{
return (bool)obj.GetValue(IsVisibleProperty);
}
public static void SetIsVisible(DependencyObject obj, bool value)
{
obj.SetValue(IsVisibleProperty, value);
}
public static readonly DependencyProperty IsVisibleProperty =
DependencyProperty.RegisterAttached("IsVisible", typeof(bool), typeof(ChoGridViewColumnVisibilityManager), new UIPropertyMetadata(true, OnIsVisibleChanged));
public static bool GetEnabled(DependencyObject obj)
{
return (bool)obj.GetValue(EnabledProperty);
}
public static void SetEnabled(DependencyObject obj, bool value)
{
obj.SetValue(EnabledProperty, value);
}
public static readonly DependencyProperty EnabledProperty =
DependencyProperty.RegisterAttached("Enabled", typeof(bool), typeof(ChoGridViewColumnVisibilityManager), new UIPropertyMetadata(false,
new PropertyChangedCallback(OnEnabledChanged)));
private static void OnIsVisibleChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
GridViewColumn gc = obj as GridViewColumn;
if (gc != null)
{
if (!GetIsVisible(gc))
{
gc.Width = 0;
((GridViewColumnHeader)gc.Header).IsHitTestVisible = false;
}
else
{
Dictionary<GridViewColumn, double> dict = null;
if (Contains(gc, out dict))
{
gc.Width = dict[gc];
((GridViewColumnHeader)gc.Header).IsHitTestVisible = true;
}
}
}
}
private static void OnEnabledChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
ListView view = obj as ListView;
if (view != null)
{
bool enabled = (bool)e.NewValue;
if (enabled)
{
view.Loaded += (sender, e2) =>
{
UpdateListView((ListView)sender);
};
view.TargetUpdated += (sender, e2) =>
{
UpdateListView((ListView)sender);
};
view.DataContextChanged += (sender, e2) =>
{
UpdateListView((ListView)sender);
};
}
}
}
}
}