Silverlight Validation - How to Stop User Submitting Invalid Data

I have a ChildWindow that enters some data into a textbox. Now when he clicks the submit button, I only need to close the ChildWindow if the input is valid? How can I check this? I have searched and seen many examples of how to validate a textbox, but I need to know how to check if everything is valid and let the user close the window?

+2


source to share


2 answers


I hope I understand your question correctly and not too naive.

As you already wrote, you know how to actually check the content of the form, I'll be brief:

Exist:

private void OKButton_Click(object sender, RoutedEventArgs e)
{
    this.DialogResult = true;
}

      

from the ChildWindow class , which handles the Click event of the child window. In this method, you call your check routine and set this.DialogResult if the check is correct. For example. eg:



private void OKButton_Click(object sender, RoutedEventArgs e)
{
    if (MyAweSomeValidation() == true)
    {
        this.DialogResult = true;
    }
}

      

Of course you need your own logic for MyAweSomeValidation () :)

The DialogResult property is implemented in a way that automatically closes the child window when it is set. If you do not set a value, the window will not be closed this way. But you have to tell the user why it won't close, if you can handle it, of course. :)

NTN

+1


source


If you have a data form, do something like this:



dataform1.ValidateItem();
if (!dataform1.ValidationSummary.HasErrors)
{
   dataform1.CommitEdit();
   DialogResult = true;
}

      

0


source







All Articles