Nightmare Games
Nightmare Games

Reputation: 2435

Property Binding Not Updating When ValidationRule Fails

I've got a few TextBoxes for input fields and a "Save" Button in my view. Two of the TextBoxes are required fields for saving, and I've set up a custom ValidationRule in the xaml for some visual feedback (red borders and tooltips) like so:

<TextBox ToolTip="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}">
    <TextBox.Text>
        <Binding Path="ScriptFileMap" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <v:MinimumStringLengthRule MinimumLength="1" ErrorMessage="Map is required for saving." />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

The "Save" Button is linked to a DelegateCommand which calls the SaveScript() function. The function doesn't allow the user to save if the properties of the two required fields are empty:

public void SaveScript()
{
    if (this.ScriptFileName.Length > 0 && this.ScriptFileMap.Length > 0)
    {
        // save function logic
    }
}

However, the function still allows the file to be saved. On closer inspection, I see that the values of those two fields (ScriptFileName and ScriptFileMap) are not being updated when the ValidationRule fails, and it goes by the last known value.

Is this the expected behavior for ValidationRule or do I have something missing or a glitch somewhere? If the former, is there a way to override that behavior? I can't prevent the saving in the ViewModel if the empty string is never passed into the bound property.

Upvotes: 9

Views: 4607

Answers (3)

Dean Kuga
Dean Kuga

Reputation: 12131

You should implement CanExecute method and RaiseCanExecuteChanged event which will keep your button disabled until all the required properties pass the validation logic.

Upvotes: 2

Nightmare Games
Nightmare Games

Reputation: 2435

Since I never got the ValidationRule workling properly, I took a different approach and just used a number of bindings. Here's my textbox, with bindings for the text, border, and tooltip:

<TextBox Text="{Binding Path=ScriptFileName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" BorderBrush="{Binding Path=ScriptFileNameBorder, UpdateSourceTrigger=PropertyChanged}" ToolTip="{Binding Path=ScriptFileNameToolTip, UpdateSourceTrigger=PropertyChanged}" />

Here's my binding for the text field, with logic to update the border and tooltip myself (sans validation):

public string ScriptFileName
        {
            get
            {
                return this.scriptFileName;
            }

            set
            {
                this.scriptFileName = value;
                RaisePropertyChanged(() => ScriptFileName);

                if (this.ScriptFileName.Length > 0)
                {
                    this.ScriptFileNameBorder = borderBrushNormal;
                    this.scriptFileNameToolTip.Content = "Enter the name of the file.";
                }
                else
                {
                    this.ScriptFileNameBorder = Brushes.Red;
                    this.scriptFileNameToolTip.Content = "File name is required for saving.";
                }
            }
        }

Doing it this way allows me to have the user feedback I want (red borders and a tooltip message) when the box is left empty and still use the code in my SaveScript function to prevent the Save button from working.

It's a bit more typing, since I then need to have separate properties for each additional field I want to make required, but everything else I've tried either had no effect or broke something else in the program (including ValidationRules and DataTriggers).

Upvotes: 1

Mike Strobel
Mike Strobel

Reputation: 25623

Yes, that is the expected behavior. By default, validation rules run on the raw proposed value, i.e., the value before it gets converted and written back to the binding source.

Try changing the ValidationStep on your rule to UpdatedValue. That should force the rule to run after the new value is converted and written back.

Upvotes: 9

Related Questions