root/platform/silverlight/Paint/trunk/src/Paint/Controls/ColorPalette.xaml.cs @ 9437

Revision 9437, 4.3 kB (checked in by coma2n, 5 years ago)

platform/silverlight/Paint/trunk: initial import

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.Linq;
27using System.Windows;
28using System.Windows.Media;
29using System.Windows.Controls;
30using System.Diagnostics;
31
32#endregion
33
34namespace Paint.Controls {
35        /// <summary>
36        ///
37        /// </summary>
38    public partial class ColorPalette : UserControl {
39
40        #region const
41
42        /// <summary>
43        /// ブラシの一覧
44        /// </summary>
45        private static readonly Brush[] brushes;
46
47                #endregion
48
49                #region fields
50
51                /// <summary>
52        /// SelectedBrush
53        /// </summary>
54        public static readonly DependencyProperty SelectedBrushProperty = DependencyProperty.Register(
55                        "SelectedBrush", typeof(Brush), typeof(ColorPalette),
56            (d, e) => ((ColorPalette)d).colorList.SelectedItem = (Brush)e.NewValue
57        );
58
59        #endregion
60
61        #region properties
62
63        /// <summary>
64        /// 選択しているブラシを取得、設定します。
65        /// </summary>
66        public Brush SelectedBrush {
67            get {
68                return (Brush)this.GetValue(SelectedBrushProperty);
69            }
70            set {
71                this.SetValue(SelectedBrushProperty, value);
72            }
73        }
74
75        #endregion
76
77        #region event
78
79        /// <summary>
80        /// 選択しているブラシが変更された時に呼び出されます。
81        /// </summary>
82        public event SelectionChangedEventHandler SelectionChanged;
83        /// <summary>
84        /// SelectionChangedイベントを呼び出します。
85        /// </summary>
86        /// <param name="e"></param>
87        protected virtual void OnSelectionChanged(SelectionChangedEventArgs e) {
88            if(SelectionChanged != null) SelectionChanged(this, e);
89        }
90
91        #endregion
92
93        #region constructors
94
95        /// <summary>
96        /// クラスイニシャライザ
97        /// </summary>
98        static ColorPalette() {
99            var result = from prop in typeof(Colors).GetProperties()
100                         where prop.Name != "Transparent"
101                         select new SolidColorBrush(
102                             (Color)prop.GetValue(null, null)
103                         );
104
105            brushes = result.ToArray();
106        }
107
108        /// <summary>
109        /// 標準的なコンストラクタ
110        /// </summary>
111        public ColorPalette() {
112            InitializeComponent();
113
114            colorList.ItemsSource = brushes;
115        }
116
117        #endregion
118
119        #region handlers
120
121        private void colorList_SelectionChanged(object sender, SelectionChangedEventArgs e) {
122                        if(e.AddedItems.Count > 0) {
123                                SelectedBrush = (Brush)e.AddedItems[0];
124
125                                OnSelectionChanged(e);
126                        }
127        }
128
129        #endregion
130
131    }
132}
Note: See TracBrowser for help on using the browser.