Remove close button in an Extended WPF Toolkit style

1.1k views Asked by At

I am using the Extended WPF Toolkit message box here: http://wpftoolkit.codeplex.com/wikipage?title=MessageBox&referringTitle=Home but I'm not sure how to remove the close button from the MessageBox type - I don't want the user to close the MessageBox at all.

Thanks for the help!

EDIT: If i create a style setter in code like this:

System.Windows.Style style = new Style();
style.Setters.Add(new Setter(Xceed.Wpf.Toolkit.MessageBox.CloseButtonVisibilityProperty, 
Visibility.Hidden));

messageBox.Style = style;

I get an exception:

An exception of type 'System.InvalidOperationException' occurred in Xceed.Wpf.Toolkit.dll but was not handled in user code

Additional information: Close button on MessageBox is always Visible.

3

There are 3 answers

1
Tom Gothorp On

According to your link there is a CloseButtonVisibility property which can get or set the visibility of the close button, Try setting it to 'false'

0
Richard Bailey On

Based on the above answer given by @trevor-handley, but making use of XAML within a resource dictionary:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:Xceed.Wpf.Toolkit;assembly=DotNetProjects.Wpf.Extended.Toolkit">
   ...
    <Style x:Key="MessageBoxCloseButtonStyle" TargetType="Button">
        <Setter Property="IsEnabled" Value="False"/>
        <Setter Property="Opacity" Value="0.0"/>
    </Style>
    <Style x:Key="MessageBoxStyle1" TargetType="{x:Type local:MessageBox}">
        <Setter Property="BorderBrush" Value="{StaticResource Blue}" />
        <Setter Property="CaptionForeground" Value="{StaticResource White}" />
        <Setter Property="WindowBorderBrush" Value="{StaticResource Blue}" />
        <Setter Property="WindowBackground" Value="{StaticResource Blue}" />
        <Setter Property="OkButtonStyle" Value="{StaticResource PrimaryButtonStyle}" />
        <Setter Property="YesButtonStyle" Value="{StaticResource PrimaryButtonStyle}" />
        <Setter Property="CloseButtonStyle" Value="{StaticResource MessageBoxCloseButtonStyle}"/>
    </Style>
    ...
</ResourceDictionary>
0
Trevor Handley On

As @Giallo has said, trying to set the Visibility property throws an exception.

In order to hide the close button you need to set the button's IsEnabled property to false, and the Opacity property to 0.0, like so:

Style closeButtonStyle = new Style(typeof(Button));
closeButtonStyle.Setters.Add(new Setter(UIElement.IsEnabledProperty, false));
closeButtonStyle.Setters.Add(new Setter(UIElement.OpacityProperty, 0.0));

Style msgBoxStyle = new Style(typeof(MessageBox));        
msgBoxStyle.Setters.Add(new Setter(WindowControl.CloseButtonStyleProperty, closeButtonStyle));

Then show your messagebox and reference the MessageBox Style that you just created:

MessageBoxResult result = MessageBox.Show(
    "Message Text", 
    "MessageBox Caption", 
    MessageBoxButton.OK, 
    MessageBoxImage.None, 
    MessageBoxResult.None, 
    msgBoxStyle);