我的要求是,要使用字符串格式插入的文本框值应为0或1我已经尝试了事件背后的代码工作正常,但我必须在xaml中使用字符串格式进行验证,而不是事件。所以帮帮我。
正在工作
<TextBox PreviewTextInput="NumberValidationTextBox" MaxLength="1">
private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-1]+");
e.Handled = regex.IsMatch(e.Text);
}
不起作用
<TextBox Text="{Binding Value,StringFormat=N2}" />
<TextBox Text="{Binding Value,StringFormat={}{0:#,#.00}}" />
<TextBox Text="{Binding Value,StringFormat={}{0:#,0.0}}"/>
您在桌子上有几个选择。
此XAML会将输入调整为仅0
或1
:
<TextBox Text="{Binding Value, UpdateSourceTrigger=PropertyChanged, StringFormat={}{0:1;1;0}}"/>
Value
必须为数字,否则StringFormat
将不起作用。