Bindable event-driven MessageBox for Silverlight

Here we present an approach allowing to standardize MessageBox-like scenarios, making them much more designer- and test-friendly.

The idea stems from ShowMessageBox attached behavior from the excellent Expression Blend Samples library. Unfortunately, practice shows that ShowMessageBox is not very usable: it's not bindable and lacks other features important for practical development. The main problem here is that the whole concept of MessageBox by itself does not live well together with other, even more important concepts like MVVM and designer\developer separation. Silverlight's MessageBox, in its current implementation, requires codebehind; to make MessageBox customizable, you'll need to implement some kind of XAML-friendly helper. To make MessageBox scenario testable, you'll need to develop some kind of pattern allowing your view to show event-driven messagebox, then return control to the model. Having this in mind, we developed an invisible FrameworkElement DisplayMessageBox to make MessageBox as much designer-friendly and test-friendly as possible, i.e.:

  1. Caption, title and other features of modal window are bindable, which is especially important if your need to localize your application - this way localization of MessageBox is as straightforward as localization of any other resource, and you don't need to change anything except XAML in order to do it.
  2. You can format and customize your MessageBox text directly in XAML using data from other controls and DataContext. No more clumsy business logics workarounds for forming a lengthy text just to output it somewhere - let designers create and change your messages without programmer's help.
  3. Completely model-friendly: modal window is shown by an event from your data (or from one of other controls). No messagebox-related codebehind at all: you'll just need to expose an event on which the MessageBox should be shown.
  4. You can define your own callback function(s) to handle reaction on different modal window closing scenarios (OK\YES\No\Cancel etc)

Practical usage:

A few comments on the attached sample. Let's suppose we have a form for editing object Person. On a button Save, you do an asynchronous save, than you need to display a MessageBox showing saved Person's data. Also, your designer needs to customize the displayed info by changing its XAML. To implement it, you should do the following:

  1. In your model, expose the event Saved triggering MessageBox
  2. Store all the data you need to display in your DataContext or controls to make it bindable
  3. If you need to perform any operation(s) after closing MessageBox, specify a callback function SavingMesBoxClosedHandler in your view.

Then, in your XAML, define something like:

            EventSource="{Binding}" EventName="Saved"
            Caption="Save info" Message="Person with Name={0} and email={1} was successfully saved"
            Parameter0="{Binding Person.Name}"
            Parameter1="{Binding Person.Email}"


  1. The following sample is just a conceptual demo. It uses standard MessageBox from Silverlight 3, but you can use any other modal window instead, including customizable MessageBoxes like the one described here or here. Even more, you can further customize its appearance directly in XAML pretty much the same way it was done for Title and Caption - just by adding other DependencyProperties to DisplayMessageBox class.
  2. Instead of single FrameworkElement DisplayMessageBox, the same behavior could be implemented using DataEventTrigger and custom Action, but it's always better to have one single class to handle it all.
  3. Of course it's possible to specify different callback functions for different modal window buttons or pass the message box result in the callback as a parameter.
  4. In the future, we'll to post a full-featured customizable MessageBox with different predefined sets of buttons and with its own DisplayMessageBox helper.

Thanks to Stanislav Spiridonov for help with implementation and to Expression Blend Samples team for great coding ideas.

displaymessageboxdemo.zip10.5 KB


Bindable event-driven MessageBox for Silverlight

very nice post.thanks for good infomation about silverlight.