Binding - disable break in property transfer

Using Exception Check

<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 introduce nonsense (for example, 1asdkfjlsdf ), the TextBox will get a red border. Good.

But as soon as I enter 0 when break debugging happens

and every time I click Continue on the debug panel.

Without debugging, this works as expected: a red frame for a value of 12093813asdf or 0 .

Question: How can I prevent this gap?

I can undo a specific exception (arrow in the screenshot), but I want breaks when this exception occurs in other places (for example, in the Logic of the model ArgumentException also throw in some methods). I can make my own exception, drop it and cancel it, but I'm not happy with that.

0
source share
1 answer

Random input hits your ExceptionValidationRule because 0983abcd not an integer. (More on this here: https://msdn.microsoft.com/en-us/library/system.windows.controls.exceptionvalidationrule%28v=vs.110%29.aspx )

When you send random nonsense, it does not perform the string-> int conversion and throws an ExceptionValidationRule to color your field. However, when you send 0, it raises your exception, which is not inside the try / catch (unhandled), and then clicks on the validationrule, which notices the previous exception and sets the field to red.

Check in this post about why your crashes, outside of debugging, don't cause crashes. It is designed to fail, elegantly, to prevent simple binding problems causing more problems. You still get the error, but it’s just hiding from you.

+1
source

Source: https://habr.com/ru/post/1243676/


All Articles