3

Validation.Error イベントの発生順序に関する限り、奇妙な動作が発生しています。こちらのドキュメントによると、データ バインディング エンジンは最初に、バインドされた要素の Validation.Errors 添付プロパティに追加された可能性のある ValidationError をすべて削除します。したがって、Removedの ValidationErrorEvent はaddedの前に発生する必要がありますが、奇妙なことに、私の場合は、 Removedイベントの前にaddedイベントが発生します。これが私が使用しているコードです。

XAML

<TextBox Grid.Row="3" Grid.Column="1" Name="txtGroupsPerRow" >
    <TextBox.Text>
        <Binding Path="StandardRack.NoOfGroupsPerRow" ValidatesOnDataErrors="True" NotifyOnValidationError="True" 
                 UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <gs:NumericValidationRule PropertyName="No Of Groups Per Row"/>
            </Binding.ValidationRules>
        </Binding> 
    </TextBox.Text>
</TextBox>

コードビハインド

private RoutedEventHandler _errorEventRoutedEventHandler;
private void UserControl_Loaded(object sender, RoutedEventArgs e) {
    _errorEventRoutedEventHandler = new RoutedEventHandler(ExceptionValidationErrorHandler);
    this.AddHandler(System.Windows.Controls.Validation.ErrorEvent, _errorEventRoutedEventHandler, true); 
}

private void UserControl_Unloaded(object sender, RoutedEventArgs e) {
    this.RemoveHandler(System.Windows.Controls.Validation.ErrorEvent, _errorEventRoutedEventHandler);
    _errorEventRoutedEventHandler = null;
}

//Added fired before Removed
private void ExceptionValidationErrorHandler(object sender, RoutedEventArgs e) {
    if (validationErrorEvent.Action == ValidationErrorEventAction.Added) {
        viewModel.AddValidationError(propertyPath, validationErrorEvent.Error.ErrorContent.ToString());
    }
    else if (validationErrorEvent.Action == ValidationErrorEventAction.Removed) {
        viewModel.RemoveValidationError(propertyPath);
    }
}

誰かがこの問題に遭遇しましたか、それとも私のコードに何か問題がありますか?

4

4 に答える 4