3

を使用して WPF で基本的なデータ検証サンプルを実行したいのですValidatesOnExceptionが、単に機能せず、viewmodelスローするとすぐValidationExceptionにプログラムがクラッシュし、「ValidationException was unhandled by user code 」と表示されます。

私のビューモデルは

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>
4

1 に答える 1

6

コードを実行しましたが、デバッガーで実行すると、例外を処理する catch ステートメントがないため、VS デバッガーはスローで停止します。

しかし、デバッグせずに起動すると、アプリケーションはクラッシュしません。編集ボックスの境界線が赤くなります。

例外を取り除きたい場合は、ViewModel を変更して、例外をスローする代わりに IDataErrorInfo インターフェイスを実装することができます。

例外がデバッグに干渉している場合は、たとえば、ArgumentException または ValidationException から派生したカスタム例外のスローを開始し、このカスタム例外がスローされてユーザーが処理されないときに VS が壊れないように構成できます。

于 2012-09-10T15:57:31.613 に答える