| 1 | #region license
|
|---|
| 2 | /*
|
|---|
| 3 | Copyright (c) 2008, Kouji Yamaguchi
|
|---|
| 4 | All rights reserved.
|
|---|
| 5 |
|
|---|
| 6 | Redistribution 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 |
|
|---|
| 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|---|
| 15 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 16 | IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|---|
| 17 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
|---|
| 18 | OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|---|
| 19 | OR 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 |
|
|---|
| 25 | using System;
|
|---|
| 26 | using System.Windows;
|
|---|
| 27 | using System.Windows.Controls;
|
|---|
| 28 |
|
|---|
| 29 | #endregion
|
|---|
| 30 |
|
|---|
| 31 | namespace Paint.Controls {
|
|---|
| 32 | /// <summary>
|
|---|
| 33 | ///
|
|---|
| 34 | /// </summary>
|
|---|
| 35 | public partial class NumericUpDown : UserControl {
|
|---|
| 36 |
|
|---|
| 37 | #region fields
|
|---|
| 38 |
|
|---|
| 39 | /// <summary>
|
|---|
| 40 | /// Value
|
|---|
| 41 | /// </summary>
|
|---|
| 42 | public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
|
|---|
| 43 | "Value", typeof(double), typeof(NumericUpDown),
|
|---|
| 44 | (d, e) => ((NumericUpDown)d).valueText.Text = e.NewValue.ToString()
|
|---|
| 45 | );
|
|---|
| 46 | /// <summary>
|
|---|
| 47 | /// Maximum
|
|---|
| 48 | /// </summary>
|
|---|
| 49 | public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register(
|
|---|
| 50 | "Maximum", typeof(double), typeof(NumericUpDown), (d, e) => { }
|
|---|
| 51 | );
|
|---|
| 52 | /// <summary>
|
|---|
| 53 | /// Minimum
|
|---|
| 54 | /// </summary>
|
|---|
| 55 | public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register(
|
|---|
| 56 | "Minimum", typeof(double), typeof(NumericUpDown), (d, e) => { }
|
|---|
| 57 | );
|
|---|
| 58 | /// <summary>
|
|---|
| 59 | /// Increment
|
|---|
| 60 | /// </summary>
|
|---|
| 61 | public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register(
|
|---|
| 62 | "Increment", typeof(double), typeof(NumericUpDown), (d, e) => { }
|
|---|
| 63 | );
|
|---|
| 64 |
|
|---|
| 65 | #endregion
|
|---|
| 66 |
|
|---|
| 67 | #region properties
|
|---|
| 68 |
|
|---|
| 69 | /// <summary>
|
|---|
| 70 | /// 値を取得、設定します。
|
|---|
| 71 | /// </summary>
|
|---|
| 72 | public double Value {
|
|---|
| 73 | get {
|
|---|
| 74 | return (double)this.GetValue(ValueProperty);
|
|---|
| 75 | }
|
|---|
| 76 | set {
|
|---|
| 77 | this.SetValue(ValueProperty, value);
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /// <summary>
|
|---|
| 82 | /// 最大値を取得、設定します。
|
|---|
| 83 | /// </summary>
|
|---|
| 84 | public double Maximum {
|
|---|
| 85 | get {
|
|---|
| 86 | return (double)this.GetValue(MaximumProperty);
|
|---|
| 87 | }
|
|---|
| 88 | set {
|
|---|
| 89 | this.SetValue(MaximumProperty, value);
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /// <summary>
|
|---|
| 94 | /// 最小値を取得、設定します。
|
|---|
| 95 | /// </summary>
|
|---|
| 96 | public double Minimum {
|
|---|
| 97 | get {
|
|---|
| 98 | return (double)this.GetValue(MinimumProperty);
|
|---|
| 99 | }
|
|---|
| 100 | set {
|
|---|
| 101 | this.SetValue(MinimumProperty, value);
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /// <summary>
|
|---|
| 106 | /// 増減値を取得、設定します。
|
|---|
| 107 | /// </summary>
|
|---|
| 108 | public double Increment {
|
|---|
| 109 | get {
|
|---|
| 110 | return (double)this.GetValue(IncrementProperty);
|
|---|
| 111 | }
|
|---|
| 112 | set {
|
|---|
| 113 | this.SetValue(IncrementProperty, value);
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | #endregion
|
|---|
| 118 |
|
|---|
| 119 | #region event
|
|---|
| 120 |
|
|---|
| 121 | /// <summary>
|
|---|
| 122 | /// 値が変更された時に呼び出されます。
|
|---|
| 123 | /// </summary>
|
|---|
| 124 | public event RoutedEventHandler ValueChanged;
|
|---|
| 125 | /// <summary>
|
|---|
| 126 | /// ValueChangedイベントを呼び出します。
|
|---|
| 127 | /// </summary>
|
|---|
| 128 | /// <param name="e"></param>
|
|---|
| 129 | protected virtual void OnValueChanged(RoutedEventArgs e) {
|
|---|
| 130 | if(ValueChanged != null) ValueChanged(this, e);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | #endregion
|
|---|
| 134 |
|
|---|
| 135 | #region constructors
|
|---|
| 136 |
|
|---|
| 137 | /// <summary>
|
|---|
| 138 | /// 標準的なコンストラクタ
|
|---|
| 139 | /// </summary>
|
|---|
| 140 | public NumericUpDown() {
|
|---|
| 141 | InitializeComponent();
|
|---|
| 142 |
|
|---|
| 143 | Value = 0;
|
|---|
| 144 | Maximum = 100;
|
|---|
| 145 | Minimum = 0;
|
|---|
| 146 | Increment = 1;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | #endregion
|
|---|
| 150 |
|
|---|
| 151 | #region handlers
|
|---|
| 152 |
|
|---|
| 153 | private void upButton_Click(object sender, RoutedEventArgs e) {
|
|---|
| 154 | if(Value + Increment > Maximum) return;
|
|---|
| 155 |
|
|---|
| 156 | Value += Increment;
|
|---|
| 157 |
|
|---|
| 158 | OnValueChanged(new RoutedEventArgs {
|
|---|
| 159 | Source = this
|
|---|
| 160 | });
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | private void downButton_Click(object sender, RoutedEventArgs e) {
|
|---|
| 164 | if(Value - Increment < Minimum) return;
|
|---|
| 165 |
|
|---|
| 166 | Value -= Increment;
|
|---|
| 167 |
|
|---|
| 168 | OnValueChanged(new RoutedEventArgs {
|
|---|
| 169 | Source = this
|
|---|
| 170 | });
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | #endregion
|
|---|
| 174 |
|
|---|
| 175 | }
|
|---|
| 176 | }
|
|---|