This project has moved and is read-only. For the latest updates, please go here.

MessageBox Image

Oct 3, 2013 at 12:51 AM
I am trying to figure out how one goes about setting the Image (Error, Exclamation etc) in code behind and in XAML. I cannot find anywhere that this might be explained. I know about the SystemIcons but they are objects and not ImageSource. I guess I caould create the MessageBox they way I used to with the Microsoft message box, but why have a nice WPF version if I cannot use it? There is some key piece of information I am missing and I am hoping someone can fill me in. I am using .NET 4 and Visual Studio 2012.

TIA,
Doug
Developer
Oct 3, 2013 at 1:39 PM
Hi,

If you want to set your own Image in a messageBox in XAML, you can to this :
<Grid>
         <Button Content="My Button" Click="Button_Click" Width="200" Height="22"/>
         <xctk:WindowContainer>
            <xctk:MessageBox x:Name="_messageBox"
                             ImageSource="Bold16.png"
                             Text="This is a MessageBox"
                             Caption="Message Box Title" />
         </xctk:WindowContainer>
   </Grid>

private void Button_Click( object sender, RoutedEventArgs e )
    {
      _messageBox.ShowMessageBox();
    }
If you want to set a predefined image in a messageBox in code-behind, you can do this :
 <Grid>
         <Button Content="My Button" Click="Button_Click" Width="200" Height="22"/>
         <xctk:WindowContainer>
            <xctk:MessageBox x:Name="_messageBox" />
         </xctk:WindowContainer>
   </Grid>

private void Button_Click( object sender, RoutedEventArgs e )
    {
      _messageBox.ShowMessageBox( "This is a MessageBox", "MessageBox Title", MessageBoxButton.OK, MessageBoxImage.Exclamation );
    }
If you want to set your own image in a messageBox in code-behind, you can do this :
<Grid>
         <Button Content="My Button" Click="Button_Click" Width="200" Height="22"/>
         <xctk:WindowContainer>
            <xctk:MessageBox x:Name="_messageBox" />
         </xctk:WindowContainer>
   </Grid>

private void Button_Click( object sender, RoutedEventArgs e )
    {
      _messageBox.ImageSource = new BitmapImage( new Uri( String.Format( "/WpfApplication50;component/Bold16.png" ), UriKind.RelativeOrAbsolute ) );
      _messageBox.Caption = "MessageBox Title";
      _messageBox.Text = "This is a MessageBox";
      _messageBox.ShowMessageBox();
    }
Oct 3, 2013 at 4:15 PM
Awesome! And simple. I should have been able to figure this one out!

Thanks again!

Doug
Apr 15, 2014 at 8:03 AM
Hi BoucherS,

First of all, thanks for the service you provide in developing this free tool.

To add to the question that AeroClassics asked, is there any way that these images can be set in a style, so that we set the default icons for the messagebox for the whole application (exclamation, warning, info, error..) instead of one at a time when we instantiate the messagebox? As it stands now, I think I have to know what kind of icon I need and set the source of that icon every time I use the messagebox.

Thank you.
Developer
Apr 15, 2014 at 5:05 PM
Hi Dawit_A,

Of course you can create a style for MessageBoxes. Here's an example :
 <Style x:Key="MessageBoxStyle"
             TargetType="xctk:MessageBox">
         <Setter Property="Background"
                 Value="Green" />
         <Setter Property="ImageSource"
                 Value="Bold16.png" />
 </Style>

 <xctk:WindowContainer>
            <xctk:MessageBox x:Name="_messageBox"
                             Text="This is a MessageBox"
                             Caption="Message Box Title"
                             Style="{StaticResource MessageBoxStyle}"/>
</xctk:WindowContainer>
May 25, 2014 at 10:20 AM
Edited May 25, 2014 at 10:20 AM
OK people, this is how you change the image in code behind (at least in my version of the library where the constructor of MesageBox is internal):
var res = Xceed.Wpf.Toolkit.MessageBox.Show(
  "MsgConfirmDeleteSelectedRows",
  "MsgTltConfirm",
  MessageBoxButton.YesNo,
  MessageBoxImage.None, MessageBoxResult.No, this.FindResource("MsgBoxExclamationStyle") as Style);
<Style x:Key="MsgBoxExclamation" TargetType="{x:Type xctk:MessageBox}" BasedOn="{StaticResource MsgBoxStyle}">
 <Setter Property="ImageSource">
  <Setter.Value>
   <!-- Set value here with a type of : DrawingImage, BitmapSource, etc -->
  </Setter.Value>
 </Setter>
</Style>
"MsgBoxStyle" is the key of your normal style for the MessageBox. If you don't have any, remove the "BasedOn" attribute.
Of course there are huge problems in using a XAML icon in there, since converting to ImageSource/DrawingImage is not straightforward, but at least you can customize it...

I know that you will love me for this.
If you want to thank me, thank my God and become an Orthodox Christian! :D