프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / ConvertService / ServiceBase / Markus.Service.StationController / Controls / GridViewSelectionUtilities.cs @ c7955b40

이력 | 보기 | 이력해설 | 다운로드 (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

    
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
    }
99
}
클립보드 이미지 추가 (최대 크기: 500 MB)