WPFで数値入力ができるテキストボックス。
- 最大値・最小値の設定
- 非数値入力時に入力確定しない
- Enterで入力確定、Escapeで入力破棄
サンプルプロジェクトがあります。
xaml (View側)
<DoubleTextBox Text="{Binding DoubleValue, Mode=TwoWay,UpdateSourceTrigger=Explicit}"
MaxValue="11.4" MinValue="-5.14"/>
<IntTextBox Text="{Binding IntValue, Mode=TwoWay, UpdateSourceTrigger=Explicit}"
MaxValue="33" MinValue="-4"/>
c# (Binding Source側)
public class SourceObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public double DoubleValue
{
get { return _doubleValue; }
set
{
if(_doubleValue == value) { return; }
_doubleValue = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(DoubleValue)));
}
}
private double _doubleValue;
public double IntValue
{
get { return _intValue; }
set
{
if(_intValue == value) { return; }
_intValue = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IntValue)));
}
}
private double _intValue;
}
MVVMにもとづいて、ViewModelとBindingして使用することを前提にしています。 TextプロパティにBindingを用いない場合、NumericTextBoxとしての機能は一切動かず、ただのTextBoxです。
ikorin24 (github)