|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
namespace SkypeTRPGToolWPF
{
/// <summary>
/// numericUpDown.xaml の相互作用ロジック
/// </summary>
public partial class numericUpDown : UserControl
{
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(int), typeof(numericUpDown), new FrameworkPropertyMetadata(0,(FrameworkPropertyMetadataOptions.BindsTwoWayByDefault),new PropertyChangedCallback(OnValuablePropertyChanged), new CoerceValueCallback(CoerceValue)));
public static readonly DependencyProperty MaximumProperty =
DependencyProperty.Register("Maximum", typeof(int), typeof(numericUpDown), new FrameworkPropertyMetadata(100, (FrameworkPropertyMetadataOptions.BindsTwoWayByDefault), new PropertyChangedCallback(OnMaximumPropertyChanged), new CoerceValueCallback(CoerceMaximum)));
public static readonly DependencyProperty MininumProperty =
DependencyProperty.Register("Mininum", typeof(int), typeof(numericUpDown), new FrameworkPropertyMetadata(0, (FrameworkPropertyMetadataOptions.BindsTwoWayByDefault), new PropertyChangedCallback(OnMininumPropertyChanged), new CoerceValueCallback(CoerceMininum)));
public int Maximum
{
set
{
SetValue(MaximumProperty, value);
}
get
{
return (int)GetValue(MaximumProperty);
}
}
public int Mininum
{
set
{
SetValue(MininumProperty, value);
}
get
{
return (int)GetValue(MininumProperty);
}
}
public int Value
{
set
{
SetValue(ValueProperty, value);
}
get
{
return (int)GetValue(ValueProperty); ;
}
}
public numericUpDown()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (Value < this.Maximum)
{
Value++;
}
else
{
Value = this.Maximum;
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
if (Value > this.Mininum)
{
Value--;
}
else
{
Value = this.Mininum;
}
}
public void SetText(string text)
{
this.textBlock1.Text = text;
}
private static void OnValuablePropertyChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
{
numericUpDown n = d as numericUpDown;
n.SetText(e.NewValue.ToString());
}
private static void OnMaximumPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
private static void OnMininumPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
private static object CoerceValue(DependencyObject d, object baseValue)
{
numericUpDown n = d as numericUpDown;
int value = (int)baseValue;
if (value >= n.Maximum)
value = n.Maximum;
if (value <= n.Mininum)
value = n.Mininum;
return value;
}
private static object CoerceMaximum(DependencyObject d, object baseValue)
{
numericUpDown n = d as numericUpDown;
int value = (int)baseValue;
return value;
}
private static object CoerceMininum(DependencyObject d, object baseValue)
{
numericUpDown n = d as numericUpDown;
int value = (int)baseValue;
return value;
}
}
}
とりあえず、ボタンクリック時にも、判定してあげることで対処しました。(ついでに、最大値、最小値も依存プロパティー化)
1)バインド先からの値チェックにCoerceValueCallbackを使う
2)ユーザーコントロールの値の変化時にも値チェック処理を行う。
これでいいのかな?
2度同じ処理を行っているので、なんかもっとエレガントな方法がありそうですねー^^;
|