Check WPF elements in C #?

How can I force validation when the user clicks a button?

One would think that this should be easy, as there is always something to do if the element values โ€‹โ€‹are valid.

private void buttonOk_Click(object sender, RoutedEventArgs e)
{
    // How can I force the validation here? So that empty values are not allowed (validator doesn't allow those)

    if (Validation.GetHasError(textBoxURI) ||
        Validation.GetHasError(textBoxName)) // These return valid values if user has not changed values in fields.
        return;

    DialogResult = true;
    this.Close();
}

      

As you can guess the empty value is not valid according to my validator (if I type something into the textbox) then clear it and blur focus it will show an invalid value.

As a side note: UpdateSourceTrigger

Will not affect the related issue of not validating initial values.

+2


source to share


2 answers


You need to call UpdateSource on BindingExpression. This is a text box example where we force validation:



BindingExpression exp = textBox.GetBindingExpression(TextBox.TextProperty);
exp.UpdateSource();

      

+1


source


Have you tried using MultiTrigger or MultiDataTrigger ?

Using this, you can define your real rule ... and any other validation rule you can think of.



Check it:

0


source







All Articles