ChildWindow in Silverlight 3

SL 3 introduced the ChildWindow to allow easy creation of modal 'pop-up' windows inside our main window. Once you have added a new ChildWindow to your project you can customise its appearance & content. The example below creates a ChildWindow to be used to display simple messages to users. In a full implementation you'd probably want to customise the appearance to match the colour scheme, etc of you main app - also ensure you have scroll bars appear if there is a large amount of txt, etc.

 

 Full XAML for our message box:

<controls:ChildWindow x:Class="blog_misc_temp.CustomMessageBox"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
           Width="400" Height="300"
           Title="CustomMessageBox"
                      HasCloseButton="False">
    <Grid x:Name="LayoutRoot" Margin="2">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBlock Name="txtMessage" TextWrapping="Wrap" />       
        <Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23" Grid.Row="1" />
    </Grid>
</controls:ChildWindow>

 

Code behind:

namespace blog_misc_temp
{
    public partial class CustomMessageBox : ChildWindow
    {
        // Simple property to allow setting of the TextBloxk text.
        // An alternative would be to bind the Text propery to this and
        // implement INotifyPropertyChanged to raise a PropertyChanged
        // event in the setter.
        public string Message
        {
            get
            {
                return txtMessage.Text;
            }
            set
            {
                txtMessage.Text = value;
            }
        }

        public CustomMessageBox()
        {
            InitializeComponent();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            // Set the result to ok/accepted (i.e. not cancelled)
            // This will cause the ChildWindow to close.
            this.DialogResult = true;
        }
    }
}

 

To show a pop-up message from within our main form:

            CustomMessageBox msg = new CustomMessageBox();

            msg.Title = "This is a message box"; // Title is a built-in property of ChildWindow
            msg.Message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";

            // Show our 'modal' ChildWindow message box as we do not have a cancel
            // button we discard the returned DialogResult
            msg.Show();
 

Comments (1) -

  • X10 Home Automation

    12/14/2009 10:20:49 AM | Reply

    You not going to believe this but I have lost all day looking for some info about this.  Thanks for this, it was a interesting read and has helped me out to no end.  Thanks again,

Add comment

Loading