2

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.

3
  • 1
    you could prevent the user from inputting invalid values Commented Nov 1, 2015 at 16:26
  • @Domysee you mean something like handling textChanged and rollback the field if it cannot be parsed? I guess that could work - not exactly same behaviour but it could. Commented Nov 1, 2015 at 16:32
  • exactly, I would handle the PreviewKeyDown event and cancel if its an invalid key, but in principle its the same Commented Nov 1, 2015 at 16:40

1 Answer 1

1

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:

Sign up to request clarification or add additional context in comments.

3 Comments

Well.. the point of my question is - if there is default conversion including validation for integers why does it throw exceptions. And the answer I am after if there is a workaround for this exception without a lot of effort - answer that there is none is also valid answer(for now I can live with it, but it puzzled me nonetheless)
@wondra: "if there is default conversion including validation" -- is there? It depends on what you mean by "validation"; to me, that means user feedback and disabling of related controls (e.g. "OK" button). What do you mean? Why should your definition preclude the possibility of thrown exceptions, given that catching and handling a thrown exception is a valid technique for implementing validation?
@wondra: "if there is a workaround for this exception without a lot of effort" -- again, is there? It depends on what you mean by "workaround" and "a lot of effort". For example, I personally consider the System.Windows.Forms.MaskedTextBox work-around I mentioned in my answer to be not "a lot of effort". Without precisely defining your terms and conditions, it's impossible to know what answer would address those terms and conditions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.