Validate control manually in WPF

2019-08-11 05:08发布

Here is the situation: I have a dataset bound to a WPF window. The dataset implements the IDataErrorInfo, so when a value is changed in the window, a validation occurs for the specific property changed. During validation, I may find out there is another control in the window I need to validate. I know only the field name in the Dataset that I need to validate, and not the actual control that binds to this field. So my questions are: 1. How can I find out what control binds to a specific field in a Dataset? 2. How do I trigger validation on that control by code?

Thanks!

1条回答
Evening l夕情丶
2楼-- · 2019-08-11 05:16

Two options as I see it:

  1. I think one way to handle this is in the setter for the property that's changed, clear the property that's no longer valid (if it is indeed not valid now).
  2. Another trick might be in the setter of the 1st property to raise a PropertyChanged event on the 2nd property, to force the view to reevaluate it.

I would prefer the 1st option, since you know changing the 1st property has a chance to invalidate the other property.

查看更多
登录 后发表回答