【问题标题】:WPF - Customize the style of MessageBox in Extended WPF ToolkitWPF - 在扩展 WPF 工具包中自定义 MessageBox 的样式
【发布时间】:2016-08-20 05:38:06
【问题描述】:

为了自定义MessageBox的样式,我使用了这个工具包,我从它的官方页面复制了样式代码:

http://wpftoolkit.codeplex.com/wikipage?title=MessageBox&referringTitle=Documentation

我稍微改了一下:

<Application x:Class="TotaraEditor.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:TotaraEditor"
             xmlns:toolkit="http://schemas.xceed.com/wpf/xaml/toolkit"
             StartupUri="MainWindow.xaml"
             ShutdownMode="OnMainWindowClose">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Themes/MetroDark/MetroDark.MSControls.Core.Implicit.xaml" />
                <ResourceDictionary Source="Themes/MetroDark/MetroDark.MSControls.Toolkit.Implicit.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <SolidColorBrush x:Key="MyButtonHoverBrush" Color="#FF2D2D30" />
            <SolidColorBrush x:Key="MyButtonPressedBrush" Color="#FF03A9DD" />

            <Style x:Key="MyCloseButtonStyle" TargetType="Button">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Grid>
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualState x:Name="Normal"/>
                                        <VisualState x:Name="MouseOver">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background)">
                                                    <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{StaticResource MyButtonHoverBrush}"></DiscreteObjectKeyFrame>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="Pressed">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background)">
                                                    <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{StaticResource MyButtonPressedBrush}"></DiscreteObjectKeyFrame>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <Border x:Name="Background" CornerRadius="0,0,0,0" Background="Green">
                                    <Border Margin="1,0,1,1" BorderBrush="#59FFFFFF" BorderThickness="1" CornerRadius="0,0,1,0"/>
                                </Border>
                                <ContentPresenter x:Name="contentPresenter" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
                                <Path x:Name="path" Fill="White" Margin="0,0,0,1" Visibility="Collapsed"
                    Height="60"
        Width="7"
        Stretch="Fill"
        Opacity="1"
        Data="M 2,6 C2,6 3,6 3,6 3,6 3,5 3,5 3,5 4,5 4,5 4,5 4,6 4,6 4,6 5,6 5,6 5,6 7,6 7,6 7,6 7,5 7,5 7,5 6,5 6,5 6,5 6,4 6,4 6,4 5,4 5,4 5,4 5,2 5,2 5,2 6,2 6,2 6,2 6,1 6,1 6,1 7,1 7,1 7,1 7,0 7,0 7,0 5,0 5,0 5,0 4,0 4,0 4,0 4,1 4,1 4,1 3,1 3,1 3,1 3,0 3,0 3,0 2,0 2,0 2,0 0,0 0,0 0,0 0,1 0,1 0,1 1,1 1,1 1,1 1,2 1,2 1,2 2,2 2,2 2,2 2,4 2,4 2,4 1,4 1,4 1,4 1,5 1,5 1,5 0,5 0,5 0,5 0,6 0,6 0,6 2,6 2,6 z"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <Style TargetType="{x:Type toolkit:MessageBox}">
                <Setter Property="Background" Value="#FF1E1E1E" />
                <Setter Property="BorderBrush" Value="#FF999999" />
                <Setter Property="CaptionForeground" Value="#FFF1F1F1" />
                <Setter Property="WindowBorderBrush" Value="#FF686868" />
                <Setter Property="WindowBackground" Value="#FF2D2D30" />
                <Setter Property="WindowOpacity" Value="0.3" />
                <Setter Property="Foreground" Value="#FFF1F1F1"/>
                <Setter Property="CloseButtonStyle" Value="{StaticResource MyCloseButtonStyle}"/>
            </Style>



        </ResourceDictionary>

    </Application.Resources>
</Application>

这是我在后面的代码中使用它的方式:

var res = Xceed.Wpf.Toolkit.MessageBox.Show( 
                            "R U sure?", 
                            "Confirm dialog", 
                            MessageBoxButton.YesNoCancel, 
                            MessageBoxImage.None, 
                            MessageBoxResult.Cancel, 
                            null 
                        ); 
if ("Cancel" == res.ToString()) {...} 
else if ("No" == res.ToString()) {...} 
else if ("Yes" == res.ToString()) {...} 
else {...}

现在我得到的是:

由于无法触摸这些按钮的 XAML,我应该如何更改按钮样式?至少我想去掉头部的发光效果。

谢谢。

更新

我确定发光效果不是由应用于标签的某些样式引起的,它是标签下方的一个带样式的矩形,我在 WPF Inspector 中找到了这个:

在我将该矩形的高度设置为 0 后,那个发光的东西就消失了。

【问题讨论】:

    标签: c# wpf xaml wpftoolkit xceed


    【解决方案1】:

    您可以像这样更改按钮样式:

    private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Xceed.Wpf.Toolkit.MessageBox mbox = new Xceed.Wpf.Toolkit.MessageBox();
            mbox.OkButtonStyle = (Style)Resources["ButtonStyle1"];
            mbox.OkButtonContent = "Click Me !";
            mbox.Caption = "My custom caption";
            mbox.Text = "My custom message";
            mbox.ShowDialog();
        }
    

    XAML:

        <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
            <Setter Property="Background" Value="Green"></Setter>
            <Setter Property="Foreground" Value="White"></Setter>
        </Style>
    

    至于标题中的发光效果,您必须相应地修改从您发布的链接中复制的样式,或者创建自己的样式。

    编辑:使用MetroDark 样式:

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var res = Xceed.Wpf.Toolkit.MessageBox.Show(
                               "R U sure?",
                               "Confirm dialog",
                               MessageBoxButton.YesNoCancel,
                               MessageBoxImage.None,
                               MessageBoxResult.Cancel,
                               (Style)Resources["MessageBoxStyle1"]
                           );
    
            if ("Cancel" == res.ToString())
            {
            }
            else if ("No" == res.ToString())
            {
            }
            else if ("Yes" == res.ToString())
            {
            }
            else
            {
            }
        }
    

    XAML:

            <Style x:Key="ButtonStyle1" TargetType="Button">
                <Setter Property="Background" Value="LightGreen"></Setter>
                <Setter Property="Foreground" Value="DarkGreen"></Setter>
            </Style>
            <Style x:Key="ButtonStyle2" TargetType="Button">
                <Setter Property="Background" Value="LightCoral"></Setter>
                <Setter Property="Foreground" Value="DarkRed"></Setter>
            </Style>
            <Style x:Key="ButtonStyle3" TargetType="Button">
                <Setter Property="Background" Value="LightYellow"></Setter>
                <Setter Property="Foreground" Value="DarkOrange"></Setter>
            </Style>
    
            <Style x:Key="MessageBoxStyle1" TargetType="{x:Type xctk:MessageBox}">
                <Setter Property="Background" Value="#FF1E1E1E" />
                <Setter Property="BorderBrush" Value="#FF999999" />
                <Setter Property="CaptionForeground" Value="#FFF1F1F1" />
                <Setter Property="WindowBorderBrush" Value="#FF686868" />
                <Setter Property="WindowBackground" Value="#FF2D2D30" />
                <Setter Property="WindowOpacity" Value="0.3" />
                <Setter Property="Foreground" Value="#FFF1F1F1"/>
                <Setter Property="CloseButtonStyle" Value="{StaticResource MyCloseButtonStyle}"/>
                <Setter Property="YesButtonStyle" Value="{StaticResource ButtonStyle1}"></Setter>
                <Setter Property="NoButtonStyle"  Value="{StaticResource ButtonStyle2}"></Setter>
                <Setter Property="CancelButtonStyle" Value="{StaticResource ButtonStyle3}"></Setter>
            </Style>
    

    编辑2:标题发光链接到CaptionShadowBrush

            <Setter Property="CaptionShadowBrush" Value="LightCoral"></Setter>
    

    您可以将其设置为 Transparent,或完全删除它:

            <Setter Property="CaptionShadowBrush" Value="Transparent"></Setter>
    

    【讨论】:

    • 谢谢jstreet,我更新了问题,如你所见,在样式代码中,我没有看到任何与发光效果相关的内容,大部分代码是关于按钮样式的.
    • 另一方面,我正在使用 MessageBox 让用户做出决定:var res = Xceed.Wpf.Toolkit.MessageBox.Show( "R U sure?", "Confirm dialog", MessageBoxButton.YesNoCancel, MessageBoxImage.None, MessageBoxResult.Cancel, null ); if ("Cancel" == res.ToString()) {...} else if ("No" == res.ToString()) {...} else if ("Yes" == res.ToString()) {...} else {...}
    • @VincentZHANG 请将样式发布为文本,而不是打印屏幕,以便人们可以复制它。另外,请将代码从您的评论移到您的帖子中,然后明确说明您需要什么。
    • 好了,搞定了,样式代码和我发的链接一样。谢谢。
    • 因为您调用的是MessageBox.Show(),这是一个static 方法,您不会更改为它设置任何Style。顺便说一句,当我运行你的代码时,我得到一个普通的MessageBox,没有任何Style,所以一定有一些你没有向我们展示的东西。如果我强制您发布的样式,它与您显示的消息框的打印屏幕不完全对应;没有发光。所以你发布的代码和你得到的结果之间有些不一致。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多