【问题标题】:Strange order of firing of Validation.Error event - Added fired before RemovedValidation.Error 事件的奇怪触发顺序 - 在删除之前添加触发
【发布时间】: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);
    }
}

有没有人遇到过这个问题,还是我的代码有问题?

【问题讨论】:

  • 我也遇到了同样的问题!

标签: wpf wpf-4.0


【解决方案1】:

查看source code,在删除旧验证错误之前添加新验证错误的步骤

被仔细排序以避免在用另一个错误替换一个错误时进入“无错误”状态

【讨论】:

  • 我认为我的应用程序遇到了同样的错误。如果我在文本框中输入无效值,则事件会正确触发,然后当我输入另一个无效值时,我会首先获得 EventAction.added 事件,然后是 EventAction.removed,它将文本解析为完全有效。我该如何解决这个问题?
  • 有人知道这个吗?每个人都说他们在这个线程上遇到了同样的事情,但没有提供关于什么是错误的或什么可以帮助这种情况的洞察力。
【解决方案2】:

记住fractor的回答,您可以尝试做一些解决方法。 创建一些计数器来表示已验证控件的错误计数:

int errorCounter = 0;
private void TextBox_Error(object sender, ValidationErrorEventArgs e)
{
    var tb = sender as TextBox;
    if (tb != null)
    {
        errorCounter += (e.Action == ValidationErrorEventAction.Added) ? 1 : -1;
        //here do whatever you want to with you stored information about error
        someControl.IsEnabled = !(errorCounter > 0);
    }
}

我知道这是个老问题,但我希望它会有所帮助。

【讨论】:

    【解决方案3】:

    看了很多论坛,发现是MS 3.5到4.0的升级问题,没有直接的解决办法。 https://social.msdn.microsoft.com/Forums/vstudio/en-US/590dc1d4-045e-4bdf-a84b-d759a5903633/validationerror-giving-strange-behavior

    但是你可以使用下面的代码来做决定

    ((BindingExpressionBase)e.Error?.BindingInError).HasValidationError

    true = 如果有验证错误,否则为 false。 它对我有用。

    【讨论】:

      【解决方案4】:

      您可以使用此事件来确定错误状态的变化,但由于它们出现乱序(有充分的理由,请参阅 fractor 的答案),您应该改为阅读 Validation.HasErrors 属性。

      var hasErrors = (bool)GetValue(Validation.HasErrorProperty);

      在同一个处理程序中执行此操作,它总是正确的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-01
        • 2019-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-12
        • 2011-08-06
        • 1970-01-01
        相关资源
        最近更新 更多