Social Icons

twitter google plus linkedin rss feed

Pages

20.2.12

MessageBox, DialogBox and Popup windows in Silverlight with the ChildWindow control

I know this has been around since Silverlight 3, but I didn’t know about it! Really? I can't believe it!

So for me is new and it looks great. In the past I have created user controls for this, but this ChildWindow has even animations…

To create it is as simple as adding a new item of this class to the project:

Create a new Silverlight Child Window
And then, tweak a bit the XAML to make it look the way you need and that’s all.

To use it you just have to create it and show it like this:
MessageWindow window = new MessageWindow("Info!", string.Format("This is a ChildWindow example."), true);
window.Show();

As always, there’s a catch. Unlike the MessageBox it won’t stop the thread waiting for your reply (this is also good in some cases, I have found lately that the MessageBox dialogs are quite resource cosuming) so if you want to use the response the user has selected in your ChildWindow you should subscribe to the Close event, something like this:
MessageWindow window = new MessageWindow("Info!", string.Format("This is a ChildWindow example."), true);
window.Show();

window.Closed += (sndr, args) =>
{
    if (window.DialogResult == true)
    {
        StratexWS.GetFBAUsersAsync();
        LoadingWindow.Show();
    }
};

I have tweaked a bit the OOTB code. Mine looks like this:

ChildWindow example
By the way, there’s at least another catch, it won’t work in the constructor… it only works after the control has been loaded (http://vanderbiest.org/blog/2010/08/03/silverlight-4-childwindow-is-not-visible/)