【发布时间】:2012-05-24 15:35:26
【问题描述】:
就 Validation.Error 事件的触发顺序而言,我遇到了奇怪的行为。根据文档here,数据绑定引擎首先删除可能已添加到绑定元素的 Validation.Errors 附加属性中的任何 ValidationError。因此,Removed 的 ValidationErrorEvent 应该在 Added 之前触发,但奇怪的是在我的情况下 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);
}
}
有没有人遇到过这个问题,还是我的代码有问题?
【问题讨论】:
-
我也遇到了同样的问题!