Manually set Validation error on Silverlight control

这一生的挚爱 提交于 2019-12-25 08:39:42

问题


I have a ComboBox which I cannot bind directly to (due to this bug), so I set it's SelectedItem from a Behavior I've written. Now, if the property bound to my behavior's SelectedValue (instead of the ComboBox's SelectedValue) is invalid (e.g. nothing is selected in the comboBox, when there should be), I want to notify the ComboBox to "go red", and display an errorcode. I know this is something the Binding system (if NotifyOnValidationError=true) handles automatically (it sets the bound control's state and ErrorCode).

My question is two-fold:

  • How do I subscribe to the Binding system's notification?
  • How do I set the ComboBox's state invalid AND set the errorCode WITHOUT hacking the controltemplate, or making unnecessary attached properties?

edit: I've found Validation static class, but it only has GetHasErrors and GetErrors, while I'd need set methods for these. Is it hopeless?


回答1:


Okay, after a full day of reading forum posts about half-hack solutions for SL3 exception style validations and horrible ideas with attached properties and subclassing combobox (oh, the pain!), I've managed to combine it to a semi-working solution. :)

The trick is: a control listens on EVERY dependency property for validation notification including FrameworkElement.Tag!

Now what we need is an object implementing IDataErrorInfo to bind to the comboBox's Tag.

Since in my scenario, I'm using a Behavior to set the comboBox manually, I already have a bound data on the behavior, so all I did was bound the behavior's SelectedValue to the comboBox's Tag, and watch the fireworks!



来源:https://stackoverflow.com/questions/5078822/manually-set-validation-error-on-silverlight-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!