【问题标题】:Notification of Collection Update for IDataErrorInfo ValidationIDataErrorInfo 验证的集合更新通知
【发布时间】:2012-03-29 00:13:20
【问题描述】:

我想将集合项的更改通知 WPF 中的绑定系统,以便每当集合中的项更改时,通过 IDataErrorInfo 进行的验证都会重新评估。 我有一个实现 INotifyCollectionChanged 的​​自定义列表类型(并且可以正常工作)。但不知何故,验证逻辑永远不会被调用,因为(或者至少我假设)这个通知没有到达正确的位置。这种情况甚至可能吗?我错过了什么?

[编辑]

所以基本上“架构”如下:

  1. MVVM 基类实现 IDataErrorInfo,您可以在派生的 MVVM 类中使用 lambda 注册 DataValidators,例如:
RegisterDataValidator(() => People, () => (People.Count == 0) ? "At least one person must be specified" : null);

基类上的索引器检查注册的验证器并返回它的返回值。

  1. 我有一个SmartBindingList<T> where T: INotifyPropertyChange,它基本上是一个列表,当向其中添加项目时,它会注册项目的 PropertyChangedEvent 并通过在类本身上触发 CollectionChanged 事件来对这些事件做出反应:
private void OnSubPropertyChanged (object sender, PropertyChangedEventArgs e)
    {
        if (sender is T1)
        {
            if (CollectionChanged != null)
            {
                NotifyCollectionChangedEventArgs eventArgs = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, sender, sender);
                CollectionChanged(this, eventArgs);
            }
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(myPropertyName));
            }
        }
    }

所以这一切都很好,但是当代码在 CollectionChanged(this, eventArgs) 行上运行时,在验证方面没有任何反应。它应该正确连接,因为当我向集合中添加一些东西时,它可以完美运行。我错过了什么?

【问题讨论】:

  • 显示您的相关IDataErrorInfo 代码

标签: wpf binding user-controls idataerrorinfo


【解决方案1】:

在没有示例代码的情况下,这有点摸不着头脑,但请尝试针对已更改的属性提高 OnPropertyChanged 通知。这应该会导致重新评估验证。

【讨论】:

  • 好吧,我花了几天时间试图让文本框变成粉红色(当然有错误时)。验证不是 WPF 最简单的功能之一。
猜你喜欢
  • 1970-01-01
  • 2012-07-27
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多