tags:

views:

106

answers:

2

How can I force validation when user clicks button?

One would think this should be easy, since always you have to do something 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 invalid according to my validator (if I type something to textbox) then empty it and blur the focus it will show invalid value.

As a side note: UpdateSourceTrigger won't affect the related problem that initial values are not validated.

A: 

Did you try using MultiTrigger or MultiDataTrigger?

Using this you can define your present rule...and anyother validation rule you may come up with.

Check this out:

Shankar Ramachandran
I did read about it yes, someone made some conditional to stop the "ok" button being enabled using those...
Ciantic
If you do know how to set DialogResult to true using MultiTrigger do tell, I had a hard time doing that.
Ciantic
A: 

You need to call UpdateSource on the BindingExpression. This an example with a textbox, where we force validation to occur:

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