开发者

SilverLight required datepicker validation

开发者 https://www.devze.com 2023-02-24 17:42 出处:网络
I have an SL4 user control. It contains a DatePicker. The control exposes a DateTime dependency property (not nullable, because it\'s a required field). I\'ve bound the SelectedDate of the DatePicker

I have an SL4 user control. It contains a DatePicker. The control exposes a DateTime dependency property (not nullable, because it's a required field). I've bound the SelectedDate of the DatePicker (which is a Nullable<DateTime>) to this DateTime property of {RelativeSource Self}, as two-way. This binding works except when I enter a null date in the DatePicker. The binding is set up with ValidatesOnExceptions, so the border of the DatePicker turns to red, but in the tooltip it says "input is not in a correct format". But it should say that the field is required.

I tried a custom IValueConverter which throwed an exception (ValidationException, FormatException, InvalidOperationException. etc.) with a custom text, but they all ended up unhandled.

I know my control could implement INotifyDataErrorInfo, but the problem is that the null value doesn't even make it into my control, since my DateTime is not nullable, so there's nothing to validate.

I could easily do this thing without any binding at all. Or by binding to a hidden Nullable<DateTime> property in my control, validating that this property is not null, and exposing another DateTime property. Or by providing a ValueConverter which converts a null to Date开发者_开发问答Time.MinValue or something.

But these methods all seem like workarounds and I'd love a better solution. What's the best way to handle this?


Submitted to Connect, votes appreciated. https://connect.microsoft.com/VisualStudio/feedback/details/661318/the-binding-engine-doesnt-handle-exceptions-from-a-converter


You've basically got to make the value you're binding your DatePicker to a nullable DateTime even though null isn't a valid value.

Then you'll just have to rely on your validation logic to ensure that your application never allows the value to be processed/stored/whateverd while it's null.

0

精彩评论

暂无评论...
验证码 换一张
取 消