2014-05-20 82 views
4

我正在使用Mahapps.MetroWindow(http://mahapps.com/)來設置我的應用程序外觀,現在我正在尋找正確的方式來自定義X /關閉按鈕的外觀。默認情況下,MetroWindow將自定義樣式應用於所有三個命令按鈕。我想要匹配Windows始終使關閉按鈕紅色在鼠標上變爲紅色。自定義Mahapps.MetroWindow關閉按鈕

我到目前爲止發現的是,我可以將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文件我已經得到了我以爲我會在空白填補了XXX的風格定義爲

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

風格二傳手。由於我是Windows開發新手,我的問題是:我感興趣的屬性是什麼?我在哪裏可以找到一個瀏覽器根據給定的上下文瀏覽可用的屬性?如果我想要完成我上面描述的內容,那麼風格的價值是什麼?

+1

您可以下載[XAMLSpy(http://xamlspy.com/)並安裝它。安裝完成後,請將您的應用程序和XAMLSpy一起運行。您可以將XAMLSpy附加到您的應用,並發現它的元素及其屬性。 –

+1

我想象你正在尋找的是前景,但他們可能會使用不同的東西。 XAMLSpy將允許您在Visual Studio中將鼠標懸停在元素上,並查找其內容和屬性。 –

回答

4

這裏是用鼠標/關閉按鈕,一個繼承自定義樣式壓制作用

<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

希望幫助

+0

輝煌,謝謝!正是我需要的! – pkluz

+0

太棒了!謝謝!我正在尋找它2天 –

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>