root/platform/silverlight/Todo/trunk/src/Todo/Controls/ItemListBox.xaml.cs @ 9712

Revision 9712, 7.6 kB (checked in by coma2n, 7 years ago)

platform/silverlight/Todo/trunk: ToolTipの表示を追加した

Line 
1#region license
2/*
3Copyright (c) 2008, Kouji Yamaguchi
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7
8* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9
10* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11
12* Neither the name of coma2n nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
13
14THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
15INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
17OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
18OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
19OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20*/
21#endregion
22
23#region namespaces
24
25using System;
26using System.Windows;
27using System.Windows.Input;
28using System.Windows.Shapes;
29using System.Windows.Controls;
30using System.Collections.Generic;
31using System.Diagnostics;
32
33#endregion
34
35namespace Todo.Controls {
36    /// <summary>
37    ///
38    /// </summary>
39    public partial class ItemListBox : UserControl {
40
41        #region fields
42
43        /// <summary>
44        /// 最後にクリックした時間
45        /// </summary>
46        private DateTime lastClickTime = DateTime.MinValue;
47
48        /// <summary>
49        /// ToolTipのテンプレート
50        /// </summary>
51        private readonly ToolTip tooltipTemplate;
52
53        #endregion
54
55        #region properties
56
57        /// <summary>
58        /// ItemsSource
59        /// </summary>
60        public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(
61            "ItemsSource", typeof(IEnumerable<Item>), typeof(ItemListBox),
62            (d, e) => ((ItemListBox)d).itemList.ItemsSource = (IEnumerable<Item>)e.NewValue
63        );
64        /// <summary>
65        /// アイテムのソースを取得、設定します。
66        /// </summary>
67        public IEnumerable<Item> ItemsSource {
68            get {
69                return (IEnumerable<Item>)this.GetValue(ItemsSourceProperty);
70            }
71            set {
72                this.SetValue(ItemsSourceProperty, value);
73            }
74        }
75
76        /// <summary>
77        /// 選択しているTodoアイテムを取得します。
78        /// </summary>
79        public Item SelectedItem {
80            get { return itemList.SelectedItem as Item; }
81        }
82
83        #endregion
84
85        #region event
86
87        /// <summary>
88        /// アイテムの完了状態が変更された時に呼び出されます。
89        /// </summary>
90        public event EventHandler<ItemCompletingEventArgs> ItemCompleting;
91        /// <summary>
92        /// ItemCompletingイベントを呼び出します。
93        /// </summary>
94        /// <param name="e">イベント引数</param>
95        protected virtual void OnItemCompleting(ItemCompletingEventArgs e) {
96            if(ItemCompleting != null) ItemCompleting(this, e);
97        }
98
99        /// <summary>
100        /// アイテムがダブルクリックされた時に呼び出されます。
101        /// </summary>
102        public event RoutedEventHandler ItemDoubleClick;
103        /// <summary>
104        /// ItemDoubleClickイベントを呼び出します。
105        /// </summary>
106        /// <param name="e">イベント引数</param>
107        protected virtual void OnItemDoubleClick(RoutedEventArgs e) {
108            if(ItemDoubleClick != null) ItemDoubleClick(this, e);
109        }
110
111                /// <summary>
112                /// アイテムの選択が変更された時に呼び出されます。
113                /// </summary>
114                public event SelectionChangedEventHandler SelectionChanged;
115                /// <summary>
116                /// SelectionChangedイベントを呼び出します。
117                /// </summary>
118                /// <param name="e">イベント引数</param>
119                protected virtual void OnSelectionChanged(SelectionChangedEventArgs e) {
120                        if(SelectionChanged != null) SelectionChanged(this, e);
121                }
122
123        #endregion
124
125        #region constructors
126
127        /// <summary>
128        /// 標準的なコンストラクタ
129        /// </summary>
130        public ItemListBox() {
131            InitializeComponent();
132
133            tooltipTemplate = (ToolTip)this.Resources["tooltipTemplate"];
134        }
135
136        #endregion
137
138                #region handlers
139
140                private void itemList_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
141                        var now = DateTime.Now;
142
143                        if((now - lastClickTime).TotalMilliseconds <= 200) {
144                                if(this.SelectedItem != null) {
145                                        OnItemDoubleClick(new RoutedEventArgs {
146                                                Source = this
147                                        });
148                                }
149                        }
150                        lastClickTime = now;
151                }
152
153                private void completeCheck_Checked(object sender, RoutedEventArgs e) {
154                        var checkbox = (CheckBox)sender;
155                        var strikeLine = (Line)checkbox.FindName("strikeLine");
156
157                        strikeLine.Visibility = checkbox.IsChecked.Value ? Visibility.Visible : Visibility.Collapsed;
158
159                        var item = this.SelectedItem;
160
161                        if(item != null) {
162                                OnItemCompleting(new ItemCompletingEventArgs(item.Id, checkbox.IsChecked.Value) {
163                                        Source = this
164                                });
165                        }
166                }
167
168                private void itemList_SelectionChanged(object sender, SelectionChangedEventArgs e) {
169                        OnSelectionChanged(e);
170                }
171
172        private void Item_Loaded(object sender, RoutedEventArgs e) {
173            var ele = (FrameworkElement)sender;
174            var item = ele.DataContext as Item;
175
176            if(item != null) {
177                var tooltip = new ToolTip {
178                    Content = item.Description,
179                    Template = tooltipTemplate.Template
180                };
181                ToolTipService.SetToolTip(ele, tooltip);
182            }
183        }
184
185                #endregion
186
187    }
188
189    #region ItemCompletingEventArgs class
190
191    /// <summary>
192    /// Todoアイテムの完了状態が変更された時のイベントのイベント引数クラス
193    /// </summary>
194    public class ItemCompletingEventArgs : RoutedEventArgs {
195
196        #region fields
197
198        private string id;
199        private bool complete;
200
201        #endregion
202
203        #region properties
204
205        /// <summary>
206        /// TodoアイテムのIdを取得します。
207        /// </summary>
208        public string Id {
209            get { return id; }
210        }
211
212        /// <summary>
213        /// 完了状態を取得します。
214        /// </summary>
215        public bool Complete {
216            get { return complete; }
217        }
218
219        #endregion
220
221        #region constructors
222
223        /// <summary>
224        /// TodoアイテムのIdと完了状態を設定するコンストラクタ
225        /// </summary>
226        /// <param name="id">Id</param>
227        /// <param name="complete">完了状態</param>
228        public ItemCompletingEventArgs(string id, bool complete) {
229            this.id = id;
230            this.complete = complete;
231        }
232
233        #endregion
234
235    }
236
237    #endregion
238
239}
Note: See TracBrowser for help on using the browser.