none
Creating a MessageBox like dialog in WPF RRS feed

  • Question

  • Hi,

    I am trying to create a MessageBox-like custom control derived from Window  in WPF( with styling ) and get it to wait  and return.
    The custom control is complete but the application doesn't wait the answer/Return value from the control.

    Any idea on how to do this?

    Thanks a lot,

    Ludo
    Ludo
    • Edited by Ludo24 Wednesday, June 4, 2008 12:26 PM Added details
    Wednesday, June 4, 2008 11:58 AM

Answers

All replies

  • The ShowDialog method is for showing windows modally. This is what you need to use instead of Show.
    User Interface controls for WPF and Windows Forms at http://www.divelements.co.uk
    Wednesday, June 4, 2008 4:05 PM
  • Thanks for the quick answer,

    I already tried with the showDialog() method but I am losing all the styling.
    This is my control template for the custom control (Sorry for the formatting).

    <Style TargetType="{x:Type local:MessageBoxAlert}">

    <Setter Property="Template">

    <Setter.Value>

    <ControlTemplate TargetType="{x:Type local:MessageBoxAlert}">

    <Border Background="{TemplateBinding Background}"

        BorderBrush="{TemplateBinding BorderBrush}"

        BorderThickness="{TemplateBinding BorderThickness}"

        CornerRadius="5" Margin="2" SnapsToDevicePixels="True">

    <Grid> 

    <Grid.RowDefinitions>
        <RowDefinition Height="20"/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        
    <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

    <Grid Grid.Row="0" Grid.ColumnSpan="2" Background="LightGray">

        <Grid>

        <Grid.ColumnDefinitions>
            <ColumnDefinition/>    
            <
    ColumnDefinition Width="20"/>
        
    </Grid.ColumnDefinitions>

    <TextBlock Grid.Column="0" Margin="5, 0, 0, 0" FontWeight="Bold" FontSize="12" Opacity="1.0"     Text="{TemplateBinding   title}"/>

    <Button x:Name="PART_CloseButton" Grid.Column="1"
        HorizontalAlignment="Right" VerticalAlignment="Top"
        Template="{StaticResource CloseButtonTemplate}" Width="18"  Height="18"   FontWeight="Bold"     Foreground="#FF222222">x</Button>

    </Grid>

    </Grid>

    <ContentControl Grid.Row="1" Content="{TemplateBinding Content}"/>

    <TextBlock Grid.Row="1" Name="Message" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=Message}" />

    <Button Name="btnYes" Grid.Row="1" Grid.Column="0">Yes</Button>

    <Button Name="btnNo" Grid.Row="1" Grid.Column="1">No</Button>

    </Grid>

    </Border>

    </ControlTemplate>

    </Setter.Value>
    </Setter>
    </Style>

    In my constructor:

    ShowInTaskbar = false;
    Topmost =
    true;
    AllowsTransparency =
    true;
    Opacity = 0.8;
    WindowStyle =
    WindowStyle.None;
    ResizeMode =
    ResizeMode.NoResize;
    BorderThickness =
    new Thickness(1);
    Visibility =
    Visibility.Visible;

    But if I use ShowDialog(), my window does not appear as expected.

    Any idea?

    Thanks again,

    Ludo


    Ludo
    • Edited by Ludo24 Sunday, June 8, 2008 11:42 AM updated
    Sunday, June 8, 2008 10:51 AM
  • Is this style referenced to the new window?
    Tamir http://blogs.microsoft.co.il/blogs/tamir If your question was answered, please mark it.
    Sunday, June 8, 2008 11:02 AM
  •  Yes it is. My custom control generated the generic.xaml with the template which I modified.

    Thanks for the help
    Ludo
    Sunday, June 8, 2008 11:45 AM
  • Try to put it into App.Resources and tell if it helps
    Tamir http://blogs.microsoft.co.il/blogs/tamir
    If your question was answered, please mark it.
    Sunday, June 8, 2008 12:07 PM
  •  I tried to move it to App.Resources.
    Now that I am using ShowDialog(), how can I override it to return my result?

    Thanks
    Ludo
    Sunday, June 8, 2008 1:07 PM
  • What do you want to override? ShowDialog() return bool? variable, that represents whether OK or Cancel was clicked. You should set IsCancel and property of button for return correct results
    Tamir http://blogs.microsoft.co.il/blogs/tamir
    If your question was answered, please mark it.
    Sunday, June 8, 2008 1:12 PM
  •  I figured it out. You have been of great help. Thank you!
    Ludo
    • Marked as answer by Ludo24 Sunday, June 8, 2008 4:13 PM
    Sunday, June 8, 2008 4:13 PM
  • Check this out
    http://blogsprajeesh.blogspot.com/2009/12/wpf-messagebox-custom-control.html
    Friday, December 4, 2009 3:40 PM
  • You should check out this class I've written: MessageBoxEx
    It allows for further customizations for buttons, image, callbacks, "don't show" checkbox

    Friday, May 20, 2011 12:46 PM
  • After I couldn't find a proper wpf customizable message box I've decided to implement it myself:

    http://blogs.microsoft.co.il/blogs/arik/archive/2011/05/26/a-customizable-wpf-messagebox.aspx

    Features

    • The class WPFMessageBox has the exact same interface as the current WPF MessageBox class.
    • Implemented as a custom control, thus fully customizable via standard WPF control templates.
    • Has a default control template which looks like the standard MessageBox.
    • Supports all the common types of message boxes: Error, Warning, Question and Information.
    • Has the same “Beep” sounds as when opening a standard MessageBox.
    • Supports the same behavior when pressing the Escape button as the standard MessageBox.
    • Provides the same system menu as the standard MessageBox, including disabling the Close button when the message box is in Yes-No mode.
    • Handles right-aligned and right-to-left operating systems, same as the standard MessageBox.
    • Provides support for setting the owner window as a WinForms Form control.

     


    Arik Poznanski
    blogs.microsoft.co.il/blogs/arik

    Wednesday, May 25, 2011 9:16 PM