Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The purpose of a dialog window is to gather user input while blocking the user from returning to the main application UI. These windows are usually called modal windows. The ChildWindow class serves this purpose in Silverlight. For this demo I am using Visual Studio and a project called ChildWindowAsDialog. I will start by looking at my dialogs. I have two of them. I have a SimpleChild, which has the default UI, and then I have a PrintWindow dialog, which has a custom UI. It has a ListBox, a textbox with the Number of Copies, and some additional text.
Since you are interrogating the user with the dialog, you need a way to retrieve information from the child window. One category of information you need from the dialog is which action button did the user choose? Silverlight uses the DialogResult class for that purpose. On this Print dialog, I need to know whether they clicked the OK button or the Cancel button. The other information you need from the child window is stored in properties on the type. You can set these values via standard properties or dependency properties. In this category, I need to know the number of copies and the name of the printer to send the print job to.
I will start by looking at how you get the DialogResult. To do that, I am going to open this UseDialogResult.xaml file. And then I'll switch to my code behind. Press F7. In this first button, I'm going to launch the dialog. Now when I go to check the child's DialogResult, there are three possible values that can be stored in there. It can be true, the user click the OK button; it can be false, which means the user click the Cancel button or the Close button; or it can be null.
It's usually null when you don't show the dialog first before querying the DialogResult. So here is how you could test for that, If child.DialogResult == null. Here is one mechanism for getting the OK and Cancel button information. I am going to declare a variable up here at the class level called simpleChild. This is my dialog and then on this user click procedure, I need to register a callback function when the user closes the dialog. That's what this line of code here is doing.
When the user clicks OK or Cancel, it's going to run this second method down here. So after I register by EventHandler, I would then call the show method. The user looks at the dialog for a few minutes. They click on one of the buttons. That will trigger this event, and then down here I can check for DialogResult = true. Then I know the user clicked OK or otherwise, they clicked on Cancel. Now what I need to do down here is deregister that EventHandler; otherwise, the next time you click in the button, I'll add a second EventHandler and this will run twice.
Rather than having it in two separate methods, you can also do a lambda expression, which is an inline function. Here I am declaring my dialog and then this is my lambda expression that says I have a closed EventHandler. I am declaring the EventHandler inline-- this is lambda symbol in C#--and here's that same test code I showed you before. And then here's the child.Show. Let's see what this looks like. Press F5. On the Null dialog, I get Not shown.
On the Check DialogResult, I get the User Selected OK, and on the lambda expression, it looks the same. Now I will look at the PrintWindow class. Remember this one? It has a text box for the number of copies and a ListBox with the printer name? I am going to expose some .NET properties on my type. Press F7 to look at the code. Here is how you set what the button does. When you click on the OK button I set the DialogResult = true, and same here with the Cancel button.
I've also exposed two properties on my type, a NumberOfCopies property, which when you call the getter reads the value in the text box and turns that into an integer value. And on the setter, it assigns the value to the text box. And I also have a property called PrinterName, which returns the selected item's text property. Now to use this, I am going to go down here and choose this PropertiesOnChild and show you how I can get that information. I am declaring a variable here for my printDialog.
I'm showing my printDialog. In my printDialog close event procedure, I am checking whether the user clicked on OK or not. And if they are, then I read that PrinterName and show it in a TextBlock. I can also show you that I can send information to the dialog. Here before I show the dialog, I am setting the NumberOfCopies to 17. So I am passing information into my dialog. Press F5 to run the application. Over here, and say Get the Properties. So I will choose this second printer here, the HP LaserJet, and I have successfully read that information into the TextBlock.
When I click on this Set button, you'll see that my number 17 was passed in and set here in the text box. Now, you have seen how flexible the child window is for gathering information.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97068 Viewers
61 Video lessons · 84386 Viewers
71 Video lessons · 68602 Viewers
56 Video lessons · 101085 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.