0

Using exception validation

<TextBox>
    <Binding Path="Value"
             UpdateSourceTrigger="PropertyChanged">
        <Binding.ValidationRules>
            <ExceptionValidationRule />
        </Binding.ValidationRules>
    </Binding>
</TextBox>

for this property

int _value = 1;
public int Value
{
    get { return _value; }
    set
    {
        if (value < 1)
            throw new ArgumentException();
        _value = value;
        OnPropertyChanged();
    }
}

If I enter nonsense (e.g. 1asdkfjlsdf) TextBox will get a red border. Fine.

But as soon as I enter 0 while debugging break will occurs

and I have to click Continue on debugging bar every time.

Without debugging it work as expected: red border for either 12093813asdf or 0 value.

Question: how do I prevent that break?

I can un-check specific exception (arrow on screenshot), but I want breaks when this exception occurs in other places (e.g. in Model logic ArgumentException is thrown in some methods too). I can make my own exception, throw it and un-check it, but I am not happy with that either.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • I'm confused. You're telling it to throw an exception, but then getting upset when it throws the exception? I think you may be intending to use ValidationResults. Check this out: https://msdn.microsoft.com/library/ms753962%28v=vs.100%29.aspx – Khale_Kitha Feb 18 '16 at 15:04
  • @Khale_Kitha, point is random input (e.g. `0983abcd`) is catch by binding, while `0` goes to property and then it's sudden unhandled exception. Haven't thought about custom validation rule, thanks. Though I have no idea how to make own `ExceptionBinding`, will I be able to catch property thrown exception myself? Do you want to save me some time by showing how to do it? – Sinatr Feb 18 '16 at 15:18
  • I had too much to put here. Will attempt to post an answer that may help. – Khale_Kitha Feb 18 '16 at 15:37

1 Answers1

1

The random input is caught by your ExceptionValidationRule because 0983abcd is not an integer. (More on that, here: https://msdn.microsoft.com/en-us/library/system.windows.controls.exceptionvalidationrule%28v=vs.110%29.aspx)

When you send random nonsense, it's failing the string->int conversion and triggering the ExceptionValidationRule to color your box. However, when you send the 0, it's triggering your exception, which is not inside a try/catch (unhandled), and then pushes to the validationrule which notices the previous exception and sets the box to red.

Check the MSDN article, from my comment, for a great example on how to do a second validationrule to check for your < 1 condition. (You can replace ExceptionvalidationRule entirely with it, as well.)

Check out the answer, on this post, as to why your failures, outside of debugging, do not cause a crash. It is designed to fail, gracefully, in order to prevent simple binding issues from causing more trouble. You're still getting the error, but it's just being hidden from you.

Community
  • 1
  • 1
Khale_Kitha
  • 264
  • 1
  • 10