markus / ConvertService / ServiceBase / Markus.Service.StationController / Controls / GridViewSelectionUtilities.cs @ 80391351
이력 | 보기 | 이력해설 | 다운로드 (8.84 KB)
1 |
using System; |
---|---|
2 |
using System.Collections; |
3 |
using System.Collections.Generic; |
4 |
using System.Collections.Specialized; |
5 |
using System.Windows; |
6 |
using System.Windows.Interactivity; |
7 |
using Telerik.Windows.Controls; |
8 |
|
9 |
namespace Markus.Service.StationController.Controls |
10 |
{ |
11 |
class GridViewSelectionUtilities : Behavior<RadGridView> |
12 |
{ |
13 |
private RadGridView Grid |
14 |
{ |
15 |
get |
16 |
{ |
17 |
return AssociatedObject as RadGridView; |
18 |
} |
19 |
} |
20 |
|
21 |
public INotifyCollectionChanged SelectedItems |
22 |
{ |
23 |
get { return (INotifyCollectionChanged)GetValue(SelectedItemsProperty); } |
24 |
set { SetValue(SelectedItemsProperty, value); } |
25 |
} |
26 |
|
27 |
// Using a DependencyProperty as the backing store for SelectedItemsProperty. This enables animation, styling, binding, etc... |
28 |
public static readonly DependencyProperty SelectedItemsProperty = |
29 |
DependencyProperty.Register("SelectedItems", typeof(INotifyCollectionChanged), typeof(GridViewSelectionUtilities), new PropertyMetadata(OnSelectedItemsPropertyChanged)); |
30 |
|
31 |
|
32 |
private static void OnSelectedItemsPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs args) |
33 |
{ |
34 |
var collection = args.NewValue as INotifyCollectionChanged; |
35 |
if (collection != null) |
36 |
{ |
37 |
collection.CollectionChanged += ((GridViewSelectionUtilities)target).ContextSelectedItems_CollectionChanged; |
38 |
} |
39 |
} |
40 |
|
41 |
protected override void OnAttached() |
42 |
{ |
43 |
base.OnAttached(); |
44 |
|
45 |
Grid.SelectedItems.CollectionChanged += GridSelectedItems_CollectionChanged; |
46 |
} |
47 |
|
48 |
void ContextSelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) |
49 |
{ |
50 |
UnsubscribeFromEvents(); |
51 |
|
52 |
Transfer(SelectedItems as IList, Grid.SelectedItems); |
53 |
|
54 |
SubscribeToEvents(); |
55 |
} |
56 |
|
57 |
void GridSelectedItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) |
58 |
{ |
59 |
UnsubscribeFromEvents(); |
60 |
|
61 |
Transfer(Grid.SelectedItems, SelectedItems as IList); |
62 |
|
63 |
SubscribeToEvents(); |
64 |
} |
65 |
|
66 |
private void SubscribeToEvents() |
67 |
{ |
68 |
Grid.SelectedItems.CollectionChanged += GridSelectedItems_CollectionChanged; |
69 |
|
70 |
if (SelectedItems != null) |
71 |
{ |
72 |
SelectedItems.CollectionChanged += ContextSelectedItems_CollectionChanged; |
73 |
} |
74 |
} |
75 |
|
76 |
private void UnsubscribeFromEvents() |
77 |
{ |
78 |
Grid.SelectedItems.CollectionChanged -= GridSelectedItems_CollectionChanged; |
79 |
|
80 |
if (SelectedItems != null) |
81 |
{ |
82 |
SelectedItems.CollectionChanged -= ContextSelectedItems_CollectionChanged; |
83 |
} |
84 |
} |
85 |
|
86 |
public static void Transfer(IList source, IList target) |
87 |
{ |
88 |
if (source == null || target == null) |
89 |
return; |
90 |
|
91 |
target.Clear(); |
92 |
|
93 |
foreach (var o in source) |
94 |
{ |
95 |
target.Add(o); |
96 |
} |
97 |
} |
98 |
//private static bool isSyncingSelection; |
99 |
//private static List<Tuple<WeakReference, List<RadGridView>>> collectionToGridViews = new List<Tuple<WeakReference, List<RadGridView>>>(); |
100 |
|
101 |
//public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.RegisterAttached( |
102 |
// "SelectedItems", |
103 |
// typeof(INotifyCollectionChanged), |
104 |
// typeof(GridViewSelectionUtilities), |
105 |
// new PropertyMetadata(null, OnSelectedItemsChanged)); |
106 |
|
107 |
//public static INotifyCollectionChanged GetSelectedItems(DependencyObject obj) |
108 |
//{ |
109 |
// return (INotifyCollectionChanged)obj.GetValue(SelectedItemsProperty); |
110 |
//} |
111 |
|
112 |
//public static void SetSelectedItems(DependencyObject obj, INotifyCollectionChanged value) |
113 |
//{ |
114 |
// obj.SetValue(SelectedItemsProperty, value); |
115 |
//} |
116 |
|
117 |
//private static void OnSelectedItemsChanged(DependencyObject target, DependencyPropertyChangedEventArgs args) |
118 |
//{ |
119 |
// var gridView = (RadGridView)target; |
120 |
|
121 |
// var oldCollection = args.OldValue as INotifyCollectionChanged; |
122 |
// if (oldCollection != null) |
123 |
// { |
124 |
// gridView.SelectionChanged -= GridView_SelectionChanged; |
125 |
// oldCollection.CollectionChanged -= SelectedItems_CollectionChanged; |
126 |
// RemoveAssociation(oldCollection, gridView); |
127 |
// } |
128 |
|
129 |
// var newCollection = args.NewValue as INotifyCollectionChanged; |
130 |
// if (newCollection != null) |
131 |
// { |
132 |
// gridView.SelectionChanged += GridView_SelectionChanged; |
133 |
// newCollection.CollectionChanged += SelectedItems_CollectionChanged; |
134 |
// AddAssociation(newCollection, gridView); |
135 |
// OnSelectedItemsChanged(newCollection, null, (IList)newCollection); |
136 |
// } |
137 |
//} |
138 |
|
139 |
//private static void SelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs args) |
140 |
//{ |
141 |
// INotifyCollectionChanged collection = (INotifyCollectionChanged)sender; |
142 |
// OnSelectedItemsChanged(collection, args.OldItems, args.NewItems); |
143 |
//} |
144 |
|
145 |
//private static void GridView_SelectionChanged(object sender, SelectionChangeEventArgs args) |
146 |
//{ |
147 |
// if (isSyncingSelection) |
148 |
// { |
149 |
// return; |
150 |
// } |
151 |
|
152 |
// var collection = (IList)GetSelectedItems((RadGridView)sender); |
153 |
// foreach (object item in args.RemovedItems) |
154 |
// { |
155 |
// collection.Remove(item); |
156 |
// } |
157 |
// foreach (object item in args.AddedItems) |
158 |
// { |
159 |
// collection.Add(item); |
160 |
// } |
161 |
//} |
162 |
|
163 |
//private static void OnSelectedItemsChanged(INotifyCollectionChanged collection, IList oldItems, IList newItems) |
164 |
//{ |
165 |
// isSyncingSelection = true; |
166 |
|
167 |
// var gridViews = GetOrCreateGridViews(collection); |
168 |
// foreach (var gridView in gridViews) |
169 |
// { |
170 |
// SyncSelection(gridView, oldItems, newItems); |
171 |
// } |
172 |
|
173 |
// isSyncingSelection = false; |
174 |
//} |
175 |
|
176 |
//private static void SyncSelection(RadGridView gridView, IList oldItems, IList newItems) |
177 |
//{ |
178 |
// if (oldItems != null) |
179 |
// { |
180 |
// SetItemsSelection(gridView, oldItems, false); |
181 |
// } |
182 |
|
183 |
// if (newItems != null) |
184 |
// { |
185 |
// SetItemsSelection(gridView, newItems, true); |
186 |
// } |
187 |
//} |
188 |
|
189 |
//private static void SetItemsSelection(RadGridView gridView, IList items, bool shouldSelect) |
190 |
//{ |
191 |
// foreach (var item in items) |
192 |
// { |
193 |
// bool contains = gridView.SelectedItems.Contains(item); |
194 |
// if (shouldSelect && !contains) |
195 |
// { |
196 |
// gridView.SelectedItems.Add(item); |
197 |
// } |
198 |
// else if (contains && !shouldSelect) |
199 |
// { |
200 |
// gridView.SelectedItems.Remove(item); |
201 |
// } |
202 |
// } |
203 |
//} |
204 |
|
205 |
//private static void AddAssociation(INotifyCollectionChanged collection, RadGridView gridView) |
206 |
//{ |
207 |
// List<RadGridView> gridViews = GetOrCreateGridViews(collection); |
208 |
// gridViews.Add(gridView); |
209 |
//} |
210 |
|
211 |
//private static void RemoveAssociation(INotifyCollectionChanged collection, RadGridView gridView) |
212 |
//{ |
213 |
// List<RadGridView> gridViews = GetOrCreateGridViews(collection); |
214 |
// gridViews.Remove(gridView); |
215 |
|
216 |
// if (gridViews.Count == 0) |
217 |
// { |
218 |
// CleanUp(); |
219 |
// } |
220 |
//} |
221 |
|
222 |
//private static List<RadGridView> GetOrCreateGridViews(INotifyCollectionChanged collection) |
223 |
//{ |
224 |
// for (int i = 0; i < collectionToGridViews.Count; i++) |
225 |
// { |
226 |
// var wr = collectionToGridViews[i].Item1; |
227 |
// if (wr.Target == collection) |
228 |
// { |
229 |
// return collectionToGridViews[i].Item2; |
230 |
// } |
231 |
// } |
232 |
|
233 |
// collectionToGridViews.Add(new Tuple<WeakReference, List<RadGridView>>(new WeakReference(collection), new List<RadGridView>())); |
234 |
// return collectionToGridViews[collectionToGridViews.Count - 1].Item2; |
235 |
//} |
236 |
|
237 |
//private static void CleanUp() |
238 |
//{ |
239 |
// for (int i = collectionToGridViews.Count - 1; i >= 0; i--) |
240 |
// { |
241 |
// bool isAlive = collectionToGridViews[i].Item1.IsAlive; |
242 |
// var behaviors = collectionToGridViews[i].Item2; |
243 |
// if (behaviors.Count == 0 || !isAlive) |
244 |
// { |
245 |
// collectionToGridViews.RemoveAt(i); |
246 |
// } |
247 |
// } |
248 |
//} |
249 |
} |
250 |
} |