Creating Custom Message Box in Silverlight 3

0 comments
Creating the MessageBox child window user control:

At first we will create our own user control. Then we will use it in a sample application. Start Expression Blend and choose Control library project template (in order to generate a DLL so that it can be added to in any Silverlight application), then remove the MainPage.xaml from your solution and add a new Child Window.

In expression blend, now you can easily change the appearance of this child window. You can change the information bar back ground, change the whole layout, the power is on your hand. Removed all the default assets and included the followings in the layout root of the child window:


<StackPanel Height="23" HorizontalAlignment="Right" VerticalAlignment="Bottom" Orientation="Horizontal"> 
<Button x:Name="btnYes" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" /> 
<Button x:Name="btnNo" Content="OK" Click="OKButton_Click" Width="75" Height="23" />                 
<Button x:Name="btnCancel" Width="75" Content="Button"/>
</StackPanel>           
<TextBlock x:Name="txtMsg" Text="TextBlock" TextWrapping="Wrap" Margin="155,51,8,87"/>           
<Image x:Name="imgIcon" HorizontalAlignment="Left" Width="100" Margin="8,66,0,99"/>
Now in the code delete all the default things.Now my requirement was to show four types of information : question,warning,error message and general information. For relative icons in the left hand side of the box coded the followings:

public enum MessageBoxButtons
{
    Ok, YesNo, YesNoCancel, OkCancel
}

public enum MessageBoxIcon
{
    Question, Information, Error, None, Warning
}
So now user is having the option as per the requirement, he/she can select the message box buttons and icons to. You can modify it as per as your requirement.
We have created our message box. One last thing we have to know that which button is clicked. Implementing similar to the default message box show method, but in this method the application didn't wait for the user to close the dialog in order to complete its work. In order to overcome this issue, handle the close event of the messagebox control so that we can know the clicked button.
Declared a delegate function with one parameter of type MessageBoxResult in order to know which button was clicked.

public delegate void MessageBoxClosedDelegate(MessageBoxResult result);
public event MessageBoxClosedDelegate OnMessageBoxClosed;
public MessageBoxResult Result { getset; }
private void MessageBoxChildWindow_Closed(object sender, EventArgs e)
{
    if (OnMessageBoxClosed != null)
       OnMessageBoxClosed(this.Result);
}
READ MORE >>

0 comments: