프로젝트

일반

사용자정보

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

markus / KCOM / Controls / DataNavigation.xaml.cs @ df2e7646

이력 | 보기 | 이력해설 | 다운로드 (5.48 KB)

1
using System;
2
using System.Collections;
3
using System.Collections.Generic;
4
using System.Collections.Specialized;
5
using System.Linq;
6
using System.Text;
7
using System.Threading.Tasks;
8
using System.Windows;
9
using System.Windows.Controls;
10
using System.Windows.Data;
11
using System.Windows.Documents;
12
using System.Windows.Input;
13
using System.Windows.Media;
14
using System.Windows.Media.Imaging;
15
using System.Windows.Navigation;
16
using System.Windows.Shapes;
17

    
18
namespace KCOM.Controls
19
{
20
    /// <summary>
21
    /// DataNavigation.xaml에 대한 상호 작용 논리
22
    /// </summary>
23
    public partial class DataNavigation : UserControl
24
    {
25
        private INotifyCollectionChanged ItemsNotifyCollection;
26

    
27
        public DataNavigation()
28
        {
29
            InitializeComponent();
30
            btFirst.Click += BtFirst_Click;
31
            btNext.Click += BtNext_Click;
32
            btPrevious.Click += BtPrevious_Click;
33
            btLast.Click += BtLast_Click;
34
        }
35

    
36
        private void BtFirst_Click(object sender, RoutedEventArgs e)
37
        {
38
            CurrentItem = ItemSource.Cast<object>().First();
39
        }
40

    
41
        private void BtLast_Click(object sender, RoutedEventArgs e)
42
        {
43
            CurrentItem = ItemSource.Cast<object>().Last();
44
        }
45

    
46
        private void BtPrevious_Click(object sender, RoutedEventArgs e)
47
        {
48
            if (CurrentIndex > 0)
49
            {
50
                CurrentItem = ItemSource[CurrentIndex - 1];
51
            }
52
        }
53

    
54
        private void BtNext_Click(object sender, RoutedEventArgs e)
55
        {
56
            if (CurrentIndex < TotalCount - 1)
57
            {
58
                CurrentItem = ItemSource.Cast<object>().ToList()[CurrentIndex + 1];
59
            }
60
        }
61

    
62
        public IList ItemSource
63
        {
64
            get { return (IList)GetValue(ItemSourceProperty); }
65
            set { SetValue(ItemSourceProperty, value); }
66
        }
67

    
68
        public static readonly DependencyProperty ItemSourceProperty =
69
               DependencyProperty.RegisterAttached("ItemSource", typeof(IList), typeof(DataNavigation), new PropertyMetadata(new List<object>(), ItemSourcePropertyChanged));
70

    
71
        private static void ItemSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
72
        {
73
            var owner = d as DataNavigation;
74

    
75
            owner.ItemsNotifyCollection = e.NewValue as INotifyCollectionChanged;
76
            
77
            if(owner.ItemsNotifyCollection != null)
78
            {
79
                owner.ItemsNotifyCollection.CollectionChanged += owner.NotifyCollection_CollectionChanged;
80
            }
81

    
82
        }
83

    
84
        private void NotifyCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
85
        {
86
            TotalCount = ItemSource.Cast<object>().Count();
87

    
88
            if (TotalCount > 0)
89
            {
90
                CurrentItem = ItemSource[0];
91
                DisplayCurrentIndex = 1;
92
            }
93
        }
94

    
95
        public int TotalCount
96
        {
97
            get { return (int)GetValue(TotalCountProperty); }
98
            set { SetValue(TotalCountProperty, value); }
99
        }
100

    
101
        public static readonly DependencyProperty TotalCountProperty =
102
               DependencyProperty.RegisterAttached("TotalCount", typeof(int), typeof(DataNavigation));
103

    
104
        public int CurrentIndex
105
        {
106
            get { return (int)GetValue(CurrentIndexProperty); }
107
            set { SetValue(CurrentIndexProperty, value); }
108
        }
109
        
110
        public static readonly DependencyProperty CurrentIndexProperty =
111
               DependencyProperty.RegisterAttached("CurrentIndex", typeof(int), typeof(DataNavigation),new PropertyMetadata(0, CurrentIndexPropertyChanged));
112

    
113
        private static void CurrentIndexPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
114
        {
115
            var owner = d as DataNavigation;
116
            owner.CurrentItem = owner.ItemSource[(int)e.NewValue];
117
            owner.DisplayCurrentIndex = (int)e.NewValue + 1;
118
        }
119

    
120
        /// <summary>
121
        ///Index 입력 테스트박스와 연동
122
        /// </summary>
123
        public int DisplayCurrentIndex
124
        {
125
            get { return (int)GetValue(DisplayCurrentIndexProperty); }
126
            set { SetValue(DisplayCurrentIndexProperty, value); }
127
        }
128

    
129
        public static readonly DependencyProperty DisplayCurrentIndexProperty =
130
               DependencyProperty.RegisterAttached("DisplayCurrentIndex", typeof(int), typeof(DataNavigation), new PropertyMetadata(0, DisplayCurrentIndexPropertyChanged));
131

    
132
        private static void DisplayCurrentIndexPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
133
        {
134
            var owner = d as DataNavigation;
135

    
136
            var newValue = (int)e.NewValue;
137

    
138
            if (newValue > 0 || newValue < owner.TotalCount)
139
            {
140
                owner.CurrentIndex = (int)e.NewValue - 1;
141
            }
142
        }
143

    
144
        public object CurrentItem
145
        {
146
            get { return (object)GetValue(CurrentItemProperty); }
147
            set { SetValue(CurrentItemProperty, value); }
148
        }
149

    
150
        public static readonly DependencyProperty CurrentItemProperty =
151
               DependencyProperty.RegisterAttached("CurrentItem", typeof(object), typeof(DataNavigation),new PropertyMetadata(null, CurrentItemPropertyChanged));
152

    
153
        private static void CurrentItemPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
154
        {
155
            var owner = d as DataNavigation;
156
            owner.CurrentIndex = owner.ItemSource.IndexOf(e.NewValue);
157
        }
158
    }
159
}
클립보드 이미지 추가 (최대 크기: 500 MB)