markus / ConvertService / ServiceController / Markus.Service.StationController / Controls / GridViewSelectionUtilities.cs @ 5c387707
이력 | 보기 | 이력해설 | 다운로드 (3.08 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 |
private static void OnSelectedItemsPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs args) |
32 |
{ |
33 |
var collection = args.NewValue as INotifyCollectionChanged; |
34 |
if (collection != null) |
35 |
{ |
36 |
collection.CollectionChanged += ((GridViewSelectionUtilities)target).ContextSelectedItems_CollectionChanged; |
37 |
} |
38 |
} |
39 |
|
40 |
protected override void OnAttached() |
41 |
{ |
42 |
base.OnAttached(); |
43 |
|
44 |
Grid.SelectedItems.CollectionChanged += GridSelectedItems_CollectionChanged; |
45 |
} |
46 |
|
47 |
void ContextSelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) |
48 |
{ |
49 |
UnsubscribeFromEvents(); |
50 |
|
51 |
Transfer(SelectedItems as IList, Grid.SelectedItems); |
52 |
|
53 |
SubscribeToEvents(); |
54 |
} |
55 |
|
56 |
void GridSelectedItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) |
57 |
{ |
58 |
UnsubscribeFromEvents(); |
59 |
|
60 |
Transfer(Grid.SelectedItems, SelectedItems as IList); |
61 |
|
62 |
SubscribeToEvents(); |
63 |
} |
64 |
|
65 |
private void SubscribeToEvents() |
66 |
{ |
67 |
Grid.SelectedItems.CollectionChanged += GridSelectedItems_CollectionChanged; |
68 |
|
69 |
if (SelectedItems != null) |
70 |
{ |
71 |
SelectedItems.CollectionChanged += ContextSelectedItems_CollectionChanged; |
72 |
} |
73 |
} |
74 |
|
75 |
private void UnsubscribeFromEvents() |
76 |
{ |
77 |
Grid.SelectedItems.CollectionChanged -= GridSelectedItems_CollectionChanged; |
78 |
|
79 |
if (SelectedItems != null) |
80 |
{ |
81 |
SelectedItems.CollectionChanged -= ContextSelectedItems_CollectionChanged; |
82 |
} |
83 |
} |
84 |
|
85 |
public static void Transfer(IList source, IList target) |
86 |
{ |
87 |
if (source == null || target == null) |
88 |
return; |
89 |
|
90 |
target.Clear(); |
91 |
|
92 |
foreach (var o in source) |
93 |
{ |
94 |
target.Add(o); |
95 |
} |
96 |
} |
97 |
} |
98 |
} |