uncia
uncia

Reputation: 604

Custom MessageBox Button In WPF

Is there a way to customize button on WPF MessageBox instead "Yes" and "No", I want "Enter" or "Exit" or something like that . All web i search told it is difficult do it rather by creating a window and all but those were 3 or 4 year old answers. Right now is there any easy way to do it ?

Upvotes: 4

Views: 20402

Answers (2)

Coskun Ozogul
Coskun Ozogul

Reputation: 2487

I know this is a late answer but it can be useful.

I needed the same thing and I found this way and I wanted to share it.

I use a window for this.

In my window, I have a label for the caption and another for the message.

And three buttons (or more if needed).

Here is my window :

<Window x:Class="CapronCAD.LIB.Controls.CapronMessageBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:CapronCAD.LIB.Controls"
    mc:Ignorable="d" Height="450" Width="800" WindowStyle="None" WindowStartupLocation="CenterOwner" WindowState="Minimized" ResizeMode="NoResize" ShowInTaskbar="False" AllowsTransparency="True" Background="Transparent">
<Grid x:Name="grdMessageBox" Background="#33000B4B">
    <Border BorderBrush="Black" BorderThickness="0" HorizontalAlignment="Center" Height="250" Margin="0" VerticalAlignment="Center" Width="600" Background="White" CornerRadius="5">
        <Grid>
            <Button x:Name="btnMessageBoxYes" FontWeight="SemiBold" Content="Oui" HorizontalAlignment="Right" VerticalAlignment="Bottom" Style="{DynamicResource ResourceKey=Capron_Violet_ButtonStyle}" Margin="0,0,30,30" Width="60" Click="btnMessageBoxYes_Click"/>
            <Button x:Name="btnMessageBoxNo" FontWeight="SemiBold" Content="Non" HorizontalAlignment="Right" VerticalAlignment="Bottom" Style="{DynamicResource ResourceKey=Capron_Violet_ButtonStyle}" Margin="0,0,103,30" Width="60" Background="#FFCFC9FF" Foreground="#FF6F5DF5" Click="btnMessageBoxNo_Click"/>
            <TextBlock x:Name="tbMessageBoxCaption" FontWeight="SemiBold" Margin="30,43,10,0" TextWrapping="Wrap" Text="-" VerticalAlignment="Top" FontFamily="Poppins" FontSize="14"/>
            <TextBlock x:Name="tbMessageBoxMessage" Margin="30,80,10,0" TextWrapping="Wrap" Text="-" VerticalAlignment="Top" FontFamily="Poppins"/>
            <Image x:Name="imgMessageBoxCancel" HorizontalAlignment="Right" Height="18" Margin="0,19,30,0" VerticalAlignment="Top" Width="18" Source="/CapronCAD.LIB;component/Resources/CloseBlack.png" MouseLeftButtonUp="imgMessageBoxCancel_MouseLeftButtonUp"/>
            <Button x:Name="btnMessageBoxClose" FontWeight="SemiBold" Content="Fermer" HorizontalAlignment="Center" VerticalAlignment="Bottom" Style="{DynamicResource ResourceKey=Capron_Violet_ButtonStyle}" Margin="0,0,0,30" Visibility="Collapsed" Click="btnMessageBoxClose_Click"/>
        </Grid>
    </Border>
</Grid>

Here is the simple method which shows the window as a dialog :

  internal static System.Windows.Forms.DialogResult ShowCustomMessageBox(string message, string caption = "Default caption", bool dialog = true)
    {
        Controls.CapronMessageBox mb = new Controls.CapronMessageBox(caption, message, dialog);
        mb.Topmost = true;
        mb.WindowState = WindowState.Maximized;
        mb.ShowDialog();

        
        if (mb.DialogResult == null)
            return System.Windows.Forms.DialogResult.None;
        else if (mb.DialogResult == true)
            return System.Windows.Forms.DialogResult.Yes;
        else
            return System.Windows.Forms.DialogResult.No;
    }

Here is the code behind the window :

 public partial class CapronMessageBox: Window
{
    public CapronMessageBox(string caption, string message, bool dialog)
    {
        InitializeComponent();
        tbMessageBoxCaption.Text = caption;
        tbMessageBoxMessage.Text = message;
        if (!dialog)
        {
            btnMessageBoxClose.Visibility = Visibility.Visible;
            btnMessageBoxNo.Visibility = Visibility.Collapsed;
            btnMessageBoxYes.Visibility = Visibility.Collapsed;
        }
    }

    private void btnMessageBoxNo_Click(object sender, RoutedEventArgs e)
    {
        DialogResult = false;
    }

    private void imgMessageBoxCancel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        this.Close();
    }

    private void btnMessageBoxYes_Click(object sender, RoutedEventArgs e)
    {
        DialogResult = true;
    }

    private void btnMessageBoxClose_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }
}    

And here how I call it :

 if (UserMethods.ShowCustomMessageBox("Do you want to save changes?", "Are you sure?", false) == DialogResult.Yes)
                {
                    btnSave_Click(btnSave, null);
                }

And here is how it seems : enter image description here

Upvotes: 3

Jalal
Jalal

Reputation: 6856

You can customize WPF Toolkit Extende MessageBox. Or you can use a custom one WPFCustomMessageBox that ACB suggested.

Upvotes: 6

Related Questions