3

I have a textbox that has validation on it, in the validation checks to see if its isNullOrEmpty. The validation works but what I am having difficulty is if the user never selects the textbox and clicks save, I want the validation to run again.
I was able to accomplish this in the XAML.cs file using:

Validation.MarkInvalid(cb.GetBindingExpression(dp), validationError);

Now with MVVM I'm confused how I am able to accomplish this from the viewmodel.
Textbox in question:

        <TextBox>
            <TextBox.Text>
                <Binding Path="LastName">
                    <Binding.ValidationRules>
                        <validationRules:IsNullOrEmptyValidationRule/>
                    </Binding.ValidationRules>
                    <Binding.UpdateSourceTrigger>PropertyChanged</Binding.UpdateSourceTrigger>
                    <Binding.Mode>TwoWay</Binding.Mode>
                </Binding>
            </TextBox.Text>
        </TextBox>


Any ideas?

KrystianB
  • 502
  • 4
  • 14
  • 1
    have you looked at http://stackoverflow.com/questions/483419/force-validation-on-bound-controls-in-wpf – Jonesopolis Apr 21 '17 at 18:42
  • yes, with putting the logic directly behind the XAML it works. Needs to follow MVVM standard and can't have that code running there, needs to be in viewmodel. – KrystianB Apr 21 '17 at 18:43
  • 1
    I usually have a `ValidatableBase` object that overrides `OnPropertyChanged` and validates, adding errors to a `Dictionary`. Then bind your shown error to the dictionary, using a converter to get the error out. Then, on clicking save, you can have a method `RunFullValidation` on `ValidatableBase` that validates the entire object – Jonesopolis Apr 21 '17 at 18:46

0 Answers0