views:

670

answers:

2

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?

+1  A: 

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

ClearsTheScreen
I am using this.DialogResult = true; but the problem is with MyAweSomeValidation. As I am already doing the validation in Xaml and its showing error so how can I get that "There is something wrong" without using much code. I mean atleast without writing almost same amount of code I already written in xaml.
Tanmoy
I suppose the answer is "it depends". Care to show me / us relevant parts of your XAML?I will have to admit I seem to be not as knowledgeable about the validation mechanisms with Silverlight. One way would be judicious use of the x:Name element so you have direct access on the named element in the code-behind.Another way would be to work with ValidationExceptions that you catch in the appropiate section of code-behind. But for me it's guesswork at the moment. Sample code (XAML and / or code-behind) would be helpful.
ClearsTheScreen
As I said, I "Can" re-validate in cs file with explicitly checking every controls and re-validate it. Currently I am doing it in that way. But that seems duplicate code to me as its already getting validated in xaml. Is there any way to tell a control (Like the ChildWindow in my case) has any validation error "without explicitly checking each control" ?
Tanmoy
A: 

if you have a dataform do something like this:

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