我想用ValidatesOnException
運行在WPF基本數據驗證樣本,但它根本沒有工作,並儘快我viewmodel
拋出ValidationException
,我的程序崩潰說,ValidationException是由用戶代碼未處理。資料驗證在WPF使用ValidatesOnExceptions
我的視圖模型是
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");
}
}
}
我的XAML是
<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>
我運行你的代碼,並且當在調試器下執行時,是的,VS調試器停止執行,因爲沒有處理該異常的catch語句。但是,如果在未經調試的情況下啓動,應用程序不會崩潰 - 編輯框邊框變爲紅色。如果你想擺脫這個異常,你可以改變ViewModel來實現IDataErrorInfo接口而不是拋出異常。 – Jogy
aargghh!我花了3個小時,相信我期望VS能夠很聰明地處理這種驗證,請讓我知道是否有這樣做的任何方式是Visual Studio與停止調試器,目前我可以' t實現'IDataErrorInfo',謝謝@Jogy任何方式的回答和建議,請寫下你的評論作爲對這個問題的回答。 –