简体   繁体   中英

Remove close button in an Extended WPF Toolkit style

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.

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);

根据您的链接,有一个CloseButtonVisibility属性可以获取或设置关闭按钮的可见性,尝试将其设置为“false”

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM