Reputation: 3583
I am binding TextBox.Text to int
property:
<TextBox Text="{Binding Lines, UpdateSourceTrigger=PropertyChanged}" />
private int _lines = 10;
public int Lines
{
get { return _lines; }
set { _lines = value; }
}
everything works as expected with this simple code, there is even validation for the TextBox. There is however exception System.FormatException
thrown in the Output log. My question is:
Is there elegant way to get rid of the exception without reimplementing almost everything myself?
By everything I mean validators, convertors, etc. simply ton of code that does not do anything but call Int32.TryParse
instead of Int32.Parse
. Not that exception thrown and handled by wpf would be a big problem, but full log makes finding actual problems much more difficult.
Upvotes: 2
Views: 2885
Reputation: 70671
The question isn't all that clear, but I assume you are referring to an exception that occurs if the user enters invalid text (i.e. non-numeric, non-integer data).
AFAIK, WPF does not include a built-in control that restricts user input. So your options are:
System.Windows.Forms.MaskedTextBox
control in your WPF program, i.e. using WindowsFormsHost.TextBox
control to process the input and avoid having WPF make a failed attempt to parse the invalid text.Upvotes: 1