Roland Deschain
Roland Deschain

Reputation: 2830

Textbox binding to uint - user input validation

I have 4 user inputs with a 'submit' button, like this:

user input with submit button

The for Texboxes are bound to uint variables in the viewmodel and using the 'standard' OnPropertyChanged() way, like this for example:

    /// <summary>
    /// Width of the exported image
    /// </summary>
    public uint ImageExportWidth
    {
        get { return imageExportWidth; }
        set
        {
            if (value > 0 && value < 10000)
            {
               imageExportWidth = value;
            }
            else
            {
                GuiCommons.ModernDialogShowMessage(GeneralDefines.SizeNotSupported, DefOmegaMessageBox.OmegaException);
            }
            OnPropertyChanged("ImageExportWidth");
        }
    }

If the user presses the submit button, the values are written to a init-file, which is later used for the export process.

If now the input is invalid, already the value in the viewmodel is not updated and the feedback in the GUI looks like this:

enter image description here

Now, I have the following two questions:

  1. Since, the conversion check (to uint) isn't done by my code, I'm guessing this is a automated .NET thing? Is that enough / good practice, or do I have to make an extra check for the input myself?
  2. If there is an invalid input and the user presses the submit button, I want to update the textboxes in the GUI to the last valid value (which I still have saved in the corresponding viewmodel properties). What is the best way to do this using the databinding already in place?

Upvotes: 1

Views: 749

Answers (2)

mm8
mm8

Reputation: 169200

Since, the conversion check (to uint) isn't done by my code, I'm guessing this is a automated .NET thing?

Well, yes, you can't set an uint property to anything else than a valid uint value. When you try to do this, the exception will be catched by the WPF binding engine.

Is that enough / good practice, or do I have to make an extra check for the input myself?

You could customize the "Value ... could not be converted" message using a ValidationRule if you want to, but you should not change the type of your source properties.

If there is an invalid input and the user presses the submit button, I want to update the textboxes in the GUI to the last valid value (which I still have saved in the corresponding viewmodel properties). What is the best way to do this using the databinding already in place?

Raise the PropertyChanged event for the source properties in the Execute method of the command that is bound to the Button.

Upvotes: 1

Robin Bennett
Robin Bennett

Reputation: 3231

I've found the best solution is for the ViewModel to wrap integer fields with a string property, and use int.TryParse to validate the new value when it is set.

Otherwise you can't validate the TextBox, as invalid values cannot sent to the ViewModel, so you don't know about them.

This is a common problem, you'll find a lot more information by searching for INotifyDataErrorInfo (which is how you should display the error message after your validation)

Upvotes: 0

Related Questions