在实现 IDataErrorInfo 时,如何在更新后(而不是之前)验证 clr 属性?

发布于 2024-08-05 02:03:03 字数 355 浏览 3 评论 0原文

我已经在我的一个类中实现了 IDataErrorInfo 来验证该类的属性。该属性绑定到我的 wpf 用户控件上的控件。验证工作正常,但存在一个重大缺陷。

它似乎在更新属性之前调用 IDataErrorInfo 成员 public string this[string columnName] ,因此当我检查正在更改的属性的值时,它包含前一个值而不是刚刚输入的值。因此,验证总是落后一步。这意味着我无法检查刚刚输入的错误值。

有没有什么方法可以强制在更新属性之后而不是之前调用此验证。我尝试将 UpdateSourceTrigger 更改为 LostFocus 和 PropertyChanged,但它们仍然报告以前的值,只是在不同的时间。

谢谢。

I have implemented IDataErrorInfo in one of my classes to validate a property of that class. The property is bound to a control on my wpf user control. The validataion works fine, except there is one vital flaw.

It seems to be calling the IDataErrorInfo member public string this[string columnName] before the property is updated, so when I check the value of the property being changed it contains the previous value not the one just entered. Therefore, the validation is always one step behind. This means that I can't check for incorrect values that have just been entered.

Is there any way of forcing this validation to be called after the property has been updated and not before. I have tried changing the UpdateSourceTrigger to both LostFocus and PropertyChanged but they still report the previous value, just at different times.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转瞬即逝 2024-08-12 02:03:03

在分配属性之前发送属性更改通知时,可以重现您所指的行为。

public string FirstName
{
    get { return _firstName; }
    set
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged
                (this, new PropertyChangedEventArgs("FirstName"));
        }
        _firstName = value;
    }
}

在实际设置值后调用更改通知可能会解决问题。郑重声明,我不会这样称呼属性更改通知!

The behavior that you are referring to can be reproduced when property change notifications are sent before the property is assigned.

public string FirstName
{
    get { return _firstName; }
    set
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged
                (this, new PropertyChangedEventArgs("FirstName"));
        }
        _firstName = value;
    }
}

May be calling the change notification after you have actually set the value may do the trick. And just for the record I don't call property changed notification like this!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文