1

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?
2
  • For 1: this is a default WPF validation rule based on exceptions. If you're satisfied with that, you don't need anything more. For 2: you can use BindingGroup.CancelEdit for that (e.g. read here). Commented Aug 9, 2018 at 12:16
  • @dymanoid Do I understand this correctly: This is done in the Code-Behind .cs file of the xaml, with the button press calling a function where I use BindingGroup.CancelEdit? Commented Aug 9, 2018 at 12:22

2 Answers 2

1

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.

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

1 Comment

Thanks, that is exactly what I wanted to do. Sometimes the solution is right in front of you, but you are unable to see it. :) dymanoid's comment about using BindingGroup would probably work also, but in my case this is definitely the more straight forward way.
0

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)

3 Comments

I use the TryParse function with along with NumberFormatInfo in other areas of the software where the input updates data directly (without the user pressing a button first), however here default WPF validation rule seems more convenient.
So you are suggesting that all source properties should be strings...?
@mm8 - source properties for strings, yes. Source properties should match the type that WPF is expecting, so IsEnabled should be bound to a bool, while Width should be bound to a GridLength, etc. It is possible to limit a number fields so that it's always a valid integer, but it's not very user friendly if it can never be empty, or just a period or minus sign.

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.