简体   繁体   English

Observable Collection 子项中的数据验证

[英]Data Validation in Observable Collection child

I want to have a list of properties of different types.我想要一个不同类型的属性列表。 Each property (eg BoolProperty , StringProperty ,..) derives from the PropertiesBase class.每个属性(例如BoolPropertyStringProperty 、..)都派生自PropertiesBase class。 PropertiesBase class has the member PropertyName . PropertiesBase class 具有成员PropertyName

So there exist an ObservableCollection<PropertiesBase> where a datagrids ItemSource binds to.所以存在一个ObservableCollection<PropertiesBase>数据网格 ItemSource 绑定到的地方。 I am using WPF with MVVMLight.我将 WPF 与 MVVMLight 一起使用。

If the PropertyName of a property is changed, it should be validated if the name is unique in the context of the observable collection.如果更改了属性的PropertyName ,则应验证该名称在可观察集合的上下文中是否唯一。

    private string _propertyName;
    public string PropertyName
    {
        get => _propertyName;
        set
        {
            if (NAME_IS_NOT_UNIQUE)           <---- HOW TO SOLVE THIS
            {
                throw new ArgumentException("Property name must be unique");
            }

            if (_propertyName == value)
                return;
            _propertyName = value;
            RaisePropertyChanged("PropertyName");
        }
    }

My question is now:我现在的问题是:

How can I check in the PropertiesBase member PropertyName if the name is unique?如果名称是唯一的,如何签入PropertiesBase成员PropertyName

As this collection could be loaded from JSON I am not able to hand over a reference to the collection in the child constructors.由于可以从 JSON 加载此集合,因此我无法在子构造函数中移交对集合的引用。

Thanks for any help!谢谢你的帮助!

It should be clear that a single entity cannot perform any validation based on any values in the other entities since it doesn't know anything about them.应该清楚的是,单个实体不能基于其他实体中的任何值执行任何验证,因为它对它们一无所知。

You should either perform the validation in the collection itself or in the class that instantiates the collection and keep a reference to it.您应该在集合本身或在实例化集合并保留对它的引用的 class 中执行验证。

You could for example handle the CollectionChanged event and hook up an event handler to the PropertyChanged event for each PropertiesBase that gets added to the collection.例如,您可以处理CollectionChanged事件并将事件处理程序连接到添加到集合中的每个PropertiesBasePropertyChanged事件。 You perform the validation in the error handler.您在错误处理程序中执行验证。 This should be easy since you have access to all items.这应该很容易,因为您可以访问所有项目。

When it comes to "mark" and entity as invalid, you could for example do this using an IsValid property or similar.当涉及到“标记”和实体无效时,您可以例如使用IsValid属性或类似属性来执行此操作。 If you implement the INotifyDataErrorInfo interface in your model class, you could for example raise the ErrorsChanged event whenever the IsValid gets set from the event handler.如果您在 model class 中实现INotifyDataErrorInfo接口,则可以例如在从事件处理程序设置IsValid时引发ErrorsChanged事件。

If you want to perform the validation in the setters of the model properties, you must get a reference to the collection in the model class somehow.如果要在 model 属性的设置器中执行验证,则必须以某种方式获取对 model class 中的集合的引用。 There is no way around that.没有办法解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM