验证 WPF 中的集合元素
我想知道人们如何验证 WPF 中的集合。举例来说,我有一个可观察的 ViewModel 集合,我将其绑定到网格的项目源,并且用户可以向网格添加新行并需要填充它们。
首先,我需要验证每一行,以确保填充每个 ViewModel 的必填字段。这对于每一行来说都是很好且简单的。
然而,第二级验证是针对整个集合的。例如,我想确保集合中没有两行具有相同的标识符,或者没有两行具有相同的名称。我基本上检查不同行中的重复属性。我还有更复杂的条件,我必须确保集合中至少有一项设置了某些属性。
如何获得允许我检查这些规则的验证规则,对整个集合而不是单个项目进行验证。我还想在数据网格上方打印任何验证错误,以便用户可以解决问题,并且当用户修复每个不同的规则时,消息将更新或消失。
有人有执行此操作的正确方法的经验吗?
I would like to know how people are going about validating collections in WPF. Lets say for example that I have an observable collection of ViewModels that I am binding to the items source of a grid, and the user can add new rows to the grid and needs to fill them.
First of all I need to validate on each row to ensure that required fields of each ViewModel are filled in. This is fine and simple to do for each row.
However, the second level of validation is on the collection as a whole. For example i want to ensure that no two rows of the collection have the same identifier, or that no two rows have the same name. Im basically checking for duplicate properties within different rows. I also have more complex conditions where I must ensure that there is at least one item within the collection that has some property set.
How do I get a validation rule that would allow me to check these rules, validating on the whole collection rather than the individual items. I also want to print any validation error above the datagrid so that the user can fix the problem and the message will update or disappear as the user fixes each different rule.
Anyone have any experience of the proper way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
诀窍是放置集合验证逻辑,以便在 ItemsControl 的 ItemsSource 属性更改时调用它。如果您在视图模型上使用 IDataErrorInfo,则在 ItemsSource 绑定上设置 ValidatesOnDataErrors=True,并且当绑定集合属性的名称传递到接口的错误索引器中时,运行逻辑以确定该属性是否仍然有效。如果您使用自定义验证规则,那么将规则放入 ItemsSource 绑定中应该没问题。
接下来,在视图模型中,每当发生更改集合的有效/无效状态的事件时,都会引发 ItemsSource 绑定属性的 PropertyChanged 事件。例如,如果集合需要一定数量的元素,则监听 CollectionChanged 事件。每当集合发生更改时,都会引发 ItemsSource 绑定属性的 PropertyChanged 事件。这告诉 WPF 该属性已更改,从而导致其重新生效。因此,只要集合发生更改,集合验证逻辑就会运行,如果集合无效,WPF 将显示错误装饰器,或者如果集合变得有效,WPF 将删除装饰器。
The trick is to place your collection validation logic such that it's called when the ItemsControl's ItemsSource property changes. If you're using IDataErrorInfo on your view-model, then set ValidatesOnDataErrors=True on the ItemsSource binding and, when the bound collection property's name is passed into the interface's error indexer, run the logic to determine if the property is still valid or not. If you're using custom validation rules, then putting the rules into the ItemsSource binding should be fine, to.
Next, in your view-model, raise the PropertyChanged event for the ItemsSource-bound property whenever an event occurs which changes the collection's valid/invalid state. For example, if the collection needs a certain number of elements, then listen to the CollectionChanged event. Whenever the collection changes, raise the PropertyChanged event for the ItemsSource-bound property. This tells WPF that the property changed, which leads to its revalidation. Thus, your collection validation logic will run whenever the collection changes and, if the collection is invalid, WPF displays the error adorner or, if the collection becomes valid, WPF removes the adorner.