Sunday, June 20, 2010

Child Window Control SilverLight 3

In this short post I will show you how you can use the child window control of silver light 3 and get the values from the child window control. Let us start with our code, when you add new item from the add new item option you can see the Silverlight Child window template when you click on the Silverlight category. When you add it it will generate two buttons and there click event handler and code in there click event handler, the Dialog-return true for OK and false for cancel button. Below is the main screen of the child window here you can see the I have create the First Name , Last Name , Email address and the contact Number.

Here is my code to open the child window control from the code. Here you can see that I have created the childWindow object and then registered the closed event handler for the object. The reason for registering the closed event handler is that Show() is an asynchronous call. That is, it doesn't wait for the dialog to be dismissed; it returns immediately. That's why Show() returns void rather than a DialogResult. If you want to know how the dialog was dismissed (that is, what the DialogResult value is once the dialog is closed), you handle the dialog's Closed event and check DialogResult there.

ChildWindow1 childWindow = new ChildWindow1();
childWindow.Closed += new System.EventHandler(childWindow_Closed);
childWindow.Show();

void childWindow_Closed(object sender, System.EventArgs e)
{
ChildWindow1 childWindow = (ChildWindow1)sender;
if ((childWindow.DialogResult.HasValue) && (childWindow.DialogResult.Value))
{
MessageBox.Show(string.Format("First Name: {0} \nLast Name: {1}\nEmail Address:{2}\nContact Number: {3}",childWindow.FirstName,childWindow.LastName,childWindow.EmailAddress,childWindow.ContactNumber), "Return Value", MessageBoxButton.OK);
}
}
And in the closed event handler I have check for the DialogResult for the HasValue and check for the value. The HasValue indicate whether the current dialogResult has value or not and the value property of the dialogResult which is nullable.


The I have used the properties of the child window control to get the values for the First Name, Last Name, Email address and contact Number. The value to these properties are assigned on the click event handler of the OK button and in that click event handler DialogResult is assign value true and in the cancel click event handler the value is assigned to false., these value are assigned when you add new item of child window control.Hope you get some understanding of the child window control and how to return values from the child window control. You can download the source code from here
All and any comments / bugs / suggestions are welcomed!

2 comments:

Anonymous said...

Great article, thanks.......but can u please elaborate a bit more about the following paragraph

" The reason for registering the closed event handler is that Show() is an asynchronous call. That is, it doesn't wait for the dialog to be dismissed; it returns immediately. That's why Show() returns void rather than a DialogResult . . . . "

Asim Sajjad said...

@Anonymouse: I mean that when you open the child window control using show functin then statement after the show function of the child window will be executed and itg will not wait for the child window to close.hope you get the answer.