WPF验证规则防止十进制文本录入? [英] WPF validation rule preventing decimal entry in textbox?
问题描述
我在XAML中这样定义一个WPF文本框:
I have a WPF textbox defined in XAML like this:
<Window.Resources>
<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<TextBox x:Name="upperLeftCornerLatitudeTextBox" Style="{StaticResource textBoxInError}">
<TextBox.Text>
<Binding Path="UpperLeftCornerLatitude" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:LatitudeValidationRule ValidationStep="RawProposedValue"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
正如你所看到的,我的文本框被绑定到我的业务对象的十进制属性调用UpperLeftCornerLatitude看起来像这样的:
As you can see, my textbox is bound to a decimal property on my business object called UpperLeftCornerLatitude which looks like this:
private decimal _upperLeftCornerLongitude;
public decimal UpperLeftCornerLatitude
{
get { return _upperLeftCornerLongitude; }
set
{
if (_upperLeftCornerLongitude == value)
{
return;
}
_upperLeftCornerLongitude = value;
OnPropertyChanged(new PropertyChangedEventArgs("UpperLeftCornerLatitude"));
}
}
我的用户将进入一个纬度值到这个文本框并且为了验证该条目,我已经创建了一个看起来像这样的验证规则:
My user will be entering a latitude value into this textbox and in order to validate that entry, I've created a validation rule that looks like this:
public class LatitudeValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
decimal latitude;
if (decimal.TryParse(value.ToString(), out latitude))
{
if ((latitude < -90) || (latitude > 90))
{
return new ValidationResult(false, "Latitude values must be between -90.0 and 90.0.");
}
}
else
{
return new ValidationResult(false, "Latitude values must be between -90.0 and 90.0.");
}
return new ValidationResult(true, null);
}
}
我的文本框最初开始了空,我有一个断点设定在我的验证规则的开始。我在文本框中输入1,当验证规则里面我中断调试,我可以看到,值=1。到现在为止还挺好。现在,我继续运行,并输入在文本框中小数点(所以我们应该有1了)。同样,验证规则内的调试器中断,并如预期值=1。如果我通过审定规则代码一步,我看到它传递的纬度值检查,并返回以下内容:
My textbox initially starts off empty and I have a breakpoint set at the beginning of my validation rule. I enter 1 in the textbox and when my debugger breaks inside of the validation rule, I can see that value = "1". So far so good. Now I continue running and enter a decimal point in the textbox (so we should have "1." now). Again, the debugger breaks inside of the validation rule and, as expected, value = "1.". If I step through the validation rule code, I see that it passes the latitude value check and returns the following:
new ValidationRule(true, null);
然而,一旦验证规则回报,我踏入下一行代码,我发现我对我的UpperLeftCornerLatitude属性setter的第一道防线。鼠标悬停值这里揭示,它的1的一个值,而不是1。如我期望的那样。所以很自然,当我继续运行我的代码,我最终回到文本框在1的值盯着,而不是1。如果我删除所有的断点,其效果是,我似乎无法在文字框中输入小数点。有什么明显的是我在这里失踪是造成我的二传手与即使我已经进入了1的1的值结束在文本框中?非常感谢!
However, as soon as the validation rule returns and I step into the next line of code, I find myself on the first line of my UpperLeftCornerLatitude property setter. Mousing over value here reveals that it's a value of "1" instead of "1." as I would expect. So naturally when I continue running my code, I end up back in the textbox staring at a value of "1" instead of "1.". If I remove all of the breakpoints, the effect is that I can't seem to enter a decimal point in the textbox. Is there something obvious that I'm missing here that's causing my setter to end up with a value of "1" even though I have entered "1." in the textbox? Thanks very much!
推荐答案
下面有几个方法来解决这个问题。
Here are a few ways to fix this problem
一个。您结合
<Binding Path="UpperLeftCornerLatitude" Mode="TwoWay" UpdateSourceTrigger="LostFocus">
</Binding
乙。指定延迟
的结合,将允许一段时间你输入十进制
B. Specify a Delay
for the binding that will allow for some time for you to type the decimal
<Binding Path="UpperLeftCornerLatitude" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" Delay="1000">
</Binding
℃。更改小数
到字符串
并解析它自己
ð 。写 ValueConverter
覆盖默认的转换过程。
D. Write a ValueConverter
to override the default conversion process
class DecimalConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
...
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
...
}
}
这篇关于WPF验证规则防止十进制文本录入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!