Hi, I have a ChildWindow whre user enters some data in a text field. Now when he clicks submit button I need to close the ChildWindow only if the data entered is valid? How to check that? I have searched and seen many examples for how to validate the textbox but I need to know how to see if everything is valid and let user close the window?
Well I hope I understand your question correctly and am not being overly naive.
As you have written you know how to actually validate the content of the form, I'll be brief:
There is:
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 validation routine and only set this.DialogResult if the validation returns true. E.g. like this:
private void OKButton_Click(object sender, RoutedEventArgs e)
{
if (MyAweSomeValidation() == true)
{
this.DialogResult = true;
}
}
Of course you need your own logic for MyAweSomeValidation() :)
The property DialogResult is implemented in a way that automagically closes the child window when set. If you don't set the value, the window doesn't get closed that way. But you should tell the user why it doesn't close if you handle it like this, of course. :)
HTH
if you have a dataform do something like this:
dataform1.ValidateItem();
if (!dataform1.ValidationSummary.HasErrors)
{
CommitEdit();
DialogResult = true;
}