WPF中的DataValidation使用ValidatesOnExceptions [英] DataValidation in WPF using ValidatesOnExceptions
问题描述
ValidatesOnException
在WPF中运行一个基本的数据验证示例,但它根本不起作用,一旦我的 viewmodel
throws ValidationException
,我的程序崩溃说, ValidationException未被用户代码处理。 我的查看模型是
public class MainViewModel:INotifyPropertyChanged
{
// INotifyPropertyChaned执行
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ;
public string StringValue
{
get {return stringValue; }
set
{
if(value.Length> 6)
{
//下面的行会抛出未处理的异常错误?
抛出新的ValidationException(String.Format(值的长度大于{0},value.Length));
}
stringValue = value;
this.OnPropertyChanged(StringValue);
}
}
}
我的XAML是
< StackPanel x:Name =LayoutRootBackground =White>
< TextBox x:Name =radMaskedTextInput1
Width =200
Margin =10
Text ={Binding Path = StringValue,Mode = TwoWay,NotifyOnValidationError = True,ValidatesOnExceptions = True,UpdateSourceTrigger = PropertyChanged}/>
< / StackPanel>
我运行了你的代码,当在一个调试器是的,VS调试器停止在throw,因为没有处理该异常的catch语句。
但是当没有调试时启动,应用程序不会崩溃 - 编辑框边框变红。
如果要摆脱异常,您可以更改ViewModel来实现IDataErrorInfo接口,而不是抛出异常。
如果异常干扰您的调试,您可以例如开始抛出一个派生自ArgumentException或ValidationException的自定义异常,并且配置VS在此自定义时不会中断抛出异常和用户未处理的
I want to run a basic data validation sample in WPF using ValidatesOnException
, but its simply not working, and as soon as my viewmodel
throws ValidationException
, my program crashes saying, ValidationException was unhandled by user code.
My View Model is
public class MainViewModel : INotifyPropertyChanged
{
//INotifyPropertyChaned implementation
//////////////////////////////////////
private string stringValue;
public string StringValue
{
get { return stringValue; }
set
{
if (value.Length > 6)
{
//The below line throws unhandled exception error??
throw new ValidationException(String.Format("Value's length is greater than {0}.", value.Length));
}
stringValue = value;
this.OnPropertyChanged("StringValue");
}
}
}
My XAML is
<StackPanel x:Name="LayoutRoot" Background="White">
<TextBox x:Name="radMaskedTextInput1"
Width="200"
Margin="10"
Text="{Binding Path=StringValue, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
I ran your code, and when executed under a debugger, yes, the VS debugger stops at the throw, because there is no catch statement that handles that exception.
But when started without debugging, the applications does not crash - the edit box border turns red.
If you want to get rid of the exception, you may change the ViewModel to implement IDataErrorInfo interface instead of throwing exception.
If the exception is interfering with your debugging, you could for example, start throwing a custom exception derived from ArgumentException or ValidationException, and the configure VS to not break when this custom exception is thrown and user-unhandled
这篇关于WPF中的DataValidation使用ValidatesOnExceptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!