This is an interesting question. I'm not sure that I have a complete solution, but I would like to throw out a couple of ideas.
, TextBox? : MinValue MaxValue. OnLostFocus. ( : .)
public class NumericTextBox : TextBox
{
public static readonly DependencyProperty MinValueProperty =
DependencyProperty.Register("MinValue", typeof(double), typeof(NumericTextBox), new UIPropertyMetadata(Double.MinValue));
public static readonly DependencyProperty MaxValueProperty =
DependencyProperty.Register("MaxValue", typeof(double), typeof(NumericTextBox), new UIPropertyMetadata(Double.MaxValue));
public double MinValue
{
get { return (double)GetValue(MinValueProperty); }
set { SetValue(MinValueProperty, value); }
}
public double MaxValue
{
get { return (double)GetValue(MaxValueProperty); }
set { SetValue(MaxValueProperty, value); }
}
protected override void OnLostFocus(System.Windows.RoutedEventArgs e)
{
base.OnLostFocus(e);
double value = 0;
if (Double.TryParse(this.Text, out value))
{
value = Math.Max(value, this.MinValue);
value = Math.Min(value, this.MaxValue);
}
this.Text = value.ToString();
}
}
, UpdateSourceTrigger = PropertyChanged .
, , .
- , , , , . (, OnTextChanged .)
- , , -, .