自定义Mahapps.MetroWindow关闭按钮

时间:2014-05-20 00:13:41

标签: wpf xaml mahapps.metro

我正在使用Mahapps.MetroWindow(http://mahapps.com/)来设置我的应用程序外观样式,现在我正在寻找自定义X /关闭按钮外观的正确方法。默认情况下,MetroWindow将自定义样式应用于所有三个命令按钮。我想要匹配Windows总是让鼠标上的关闭按钮变为红色变为红色。

到目前为止我发现的是,我可以将WindowCloseButtonStyle属性设置为自定义样式。我是这样做的:

<controls:MetroWindow x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    Title="MainWindow" 
    Height="350" 
    Width="525"
    WindowCloseButtonStyle="{DynamicResource RedCloseWindowButtonStyle}">
...

在单独的XAML文件中,我将样式定义为

<Style x:Key="RedCloseWindowButtonStyle"
       TargetType="{x:Type Button}"
       BasedOn="{StaticResource MetroBaseWindowButtonStyle}">
    <Setter Property="XXX"
               Value="XXX" />
</Style>

我认为我必须在样式设定器中填写XXX的空白。由于我是Windows开发新手,我的问题是:我感兴趣的属性是什么?我在哪里可以找到一个浏览器来根据给定的上下文浏览可用的属性?如果我想完成上面描述的内容,那么风格的价值是什么?

3 个答案:

答案 0 :(得分:5)

这是一个继承的自定义样式,用于关闭按钮,鼠标悬停/按下效果

<Style x:Key="MetroWindowCloseButtonStyle"
       TargetType="{x:Type Button}"
       BasedOn="{StaticResource MetroWindowButtonStyle}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid x:Name="grid"
                      Background="{TemplateBinding Background}">
                    <ContentPresenter x:Name="contentPresenter"
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      Margin="{TemplateBinding Padding}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                      RecognizesAccessKey="True"
                                      Opacity="0.75" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver"
                             Value="True">
                        <Setter TargetName="contentPresenter"
                                Property="Opacity"
                                Value="1" />
                        <Setter TargetName="grid"
                                Property="Background"
                                Value="#E04343" />
                    </Trigger>
                    <Trigger Property="IsMouseOver"
                             Value="False">
                        <Setter TargetName="contentPresenter"
                                Property="Opacity"
                                Value=".5" />
                    </Trigger>
                    <Trigger Property="IsPressed"
                             Value="True">
                        <Setter TargetName="grid"
                                Property="Background"
                                Value="#993D3D" />
                    </Trigger>
                    <Trigger Property="IsEnabled"
                             Value="false">
                        <Setter Property="Foreground"
                                Value="#ADADAD" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

您可以在github

找到原始样式

希望有所帮助

答案 1 :(得分:1)

好吧,在我的新版本中我找不到风格“MetroWindowCloseButtonStyle” 我重写了“MetroBaseWindowButtonStyle”的风格 有和额外的样式触发器: 仅当按钮名称为“PART_Close”(默认的mahapp按钮名称)时,触发器才会触发。因此,当悬停到关闭按钮时,它会将背景颜色更改为红色。

<MultiTrigger>
  <MultiTrigger.Conditions>
    <Condition Property="IsMouseOver" Value="True" />
    <Condition Property="Name" Value="PART_Close" />
  </MultiTrigger.Conditions>

  <Setter Property="Background" Value="Red" />
</MultiTrigger>

完整的风格在这里:

 <!-- base button style for min, max and close window buttons -->
 <Style x:Key="MetroBaseWindowButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Background"
            Value="{DynamicResource TransparentWhiteBrush}" />
    <Setter Property="Foreground"
            Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
    <Setter Property="HorizontalContentAlignment"
            Value="Center" />
    <Setter Property="VerticalContentAlignment"
            Value="Center" />
    <Setter Property="Padding"
            Value="1" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid Background="{TemplateBinding Background}">
                    <ContentPresenter x:Name="contentPresenter"
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      Margin="{TemplateBinding Padding}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                      RecognizesAccessKey="True"
                                      Opacity="0.75" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver"
                             Value="True">
                        <Setter TargetName="contentPresenter"
                                Property="Opacity"
                                Value="1" />
                    </Trigger>
                    <Trigger Property="IsMouseOver"
                             Value="False">
                        <Setter TargetName="contentPresenter"
                                Property="Opacity"
                                Value=".5" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver"
                 Value="True">
            <Setter Property="Background"
                    Value="#F6F6F6" />
        </Trigger>
        <Trigger Property="IsPressed"
                 Value="True">
            <Setter Property="Background"
                    Value="{DynamicResource HighlightBrush}" />
        </Trigger>
        <Trigger Property="IsEnabled"
                 Value="false">
            <Setter Property="Foreground"
                    Value="#ADADAD" />
        </Trigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsMouseOver"
                           Value="True" />
                <Condition Property="Name"
                           Value="PART_Close" />
            </MultiTrigger.Conditions>
            <Setter Property="Background"
                    Value="Red" />
        </MultiTrigger>
    </Style.Triggers>
  </Style>

答案 2 :(得分:0)

将以下内容放在App.xaml中将对样式应用红色鼠标到所有MetroWindows:

<Style x:Key="CleanCloseWindowButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource MetroWindowButtonStyle}">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="#EB2F2F" />
            <Setter Property="Foreground" Value="{DynamicResource WhiteBrush}" />
        </Trigger>
        <Trigger Property="IsPressed" Value="True">
            <Setter Property="Background" Value="#7C0000" />
        </Trigger>
    </Style.Triggers>
</Style>

<Style TargetType="{x:Type mah:WindowButtonCommands}" BasedOn="{StaticResource {x:Type mah:WindowButtonCommands}}">
    <Setter Property="LightCloseButtonStyle" Value="{StaticResource CleanCloseWindowButtonStyle}" />
    <Setter Property="DarkCloseButtonStyle" Value="{StaticResource CleanCloseWindowButtonStyle}" />
</Style>

这是基于Punker在github上使用WindowCloseButtonStyle(已过时)发布的解决方案。