2014-06-26 30 views
0

我試圖設計一個按鈕的樣式,但最終沒有顯示應用自定義樣式時,只顯示其內容(按鈕的文本)。這是我聲明我的風格:自定義按鈕樣式不起作用?

<Color x:Key="NormalColor" A="255" R="60" G ="158" B="184"/> 
    <Color x:Key="HoverColor" A="255" R="74" G ="177" B="204"/> 
    <Color x:Key="PressedColor" A="255" R="26" G ="115" B="138"/> 

    <SolidColorBrush x:Key="NormalBrush" Color="{StaticResource NormalColor}" /> 
    <SolidColorBrush x:Key="HoverBrush" Color="{StaticResource HoverColor}" /> 
    <SolidColorBrush x:Key="PressedBrush" Color="{StaticResource PressedColor}" /> 

    <Style x:Key="FlatButton" TargetType="Button"> 
     <Setter Property="OverridesDefaultStyle" Value="True"/> 
     <Setter Property="FontFamily" Value="Segoe UI"/> 
     <Setter Property="Background" Value="{StaticResource NormalBrush}"/> 
     <Setter Property="Foreground" Value="#000000"/> 

     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 

        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="Content"/> 

        <ControlTemplate.Triggers> 

         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="Background" Value="{StaticResource HoverBrush}" /> 
          <Setter Property="Foreground" Value="#000000" /> 
         </Trigger> 

         <Trigger Property="IsPressed" Value="True"> 
          <Setter Property="Background" Value="{StaticResource PressedBrush}" /> 
          <Setter Property="Foreground" Value="#000000" /> 
         </Trigger> 

         <Trigger Property="IsEnabled" Value="False"> 
          <Setter Property="Background" Value="{StaticResource NormalBrush}" /> 
          <Setter Property="Foreground" Value="#000000" /> 
          <Setter Property="Opacity" Value="0.5" />         
         </Trigger> 

        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

</Window.Resources> 

,這裏是我如何使用它:

<Button Content="Connect" Command="model:GlobalCommands.ConnectToDBCommand" Style="{StaticResource FlatButton}"/> 

誰能告訴我在哪裏,我就去問題呢? 謝謝!

回答

1

您的ControlTemplate中沒有任何東西可以使用Background。把你的ContentPresenterBorder(例如),並使用TemplateBinding綁定其Background

<Border Background="{TemplateBinding Background}"> 
    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="Content"/> 
</Border> 
1

添加到@ dkozl的回答,您應該添加TemplateBinding S到Button.ControlTemplate內的任何屬性,你可能想從Button.ControlTemplate外面設置:

<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding 
    BorderThickness}" Background="{TemplateBinding Background}"> 
    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> 
</Border> 

然後,你可以這樣做:

<Button Content="Connect" Command="model:GlobalCommands.ConnectToDBCommand" 
    Style="{StaticResource FlatButton}" BorderBrush="Blue" BordeThickness="5" /> 
+0

謝謝,你們兩個! :) –

相關問題