2015-02-05 101 views
2

我想改變鼠標懸停的樣式。背景不改變按鈕C#WPF

我的代碼是:

<Button Name="register" Content="Register" Margin="15,410,20,0" Padding="6" FontSize="18" VerticalAlignment="Top" Background="#FF0090D6" BorderBrush="#FF0090D6" Foreground="White"> 
        <Button.Style> 
         <Style TargetType="Button"> 
          <Style.Triggers> 
           <Trigger Property="IsMouseOver" Value="True"> 
            <Setter Property="Background" Value="Red"></Setter> 
           </Trigger> 
          </Style.Triggers> 
         </Style> 
        </Button.Style> 
       </Button> 

但背景是相同的默認的事情。但是,當我改變財產BorderThickness然後它的工作。

我在做什麼錯了?

+0

如果您爲該顏色設置了RGBA值,它會工作嗎? – Stilgar 2015-02-05 14:47:49

+0

@Stilgar如何設置rgba?屬性窗口確實顯示我輸入的顏色 – 2015-02-05 14:50:47

回答

5

在按鈕的默認模板,有觸發在控件模板裏面設置按鈕的背景#FFBEE6FD,自控制模板觸發有higher precedence相比風格觸發,這就是爲什麼你的觸發永遠不會奏效。

爲了達到這個目的,您必須重寫默認的按鈕模板並從中刪除該觸發器,以便應用您的樣式觸發器。

以下是將該特定觸發器註釋掉的默認模板。如果你想重寫BorderBrush,也可以從模板中刪除它。

<ControlTemplate x:Key="DefaultTemplateOfButton" TargetType="ButtonBase"> 
    <Border BorderThickness="{TemplateBinding Border.BorderThickness}" 
       BorderBrush="{TemplateBinding Border.BorderBrush}" 
       Background="{TemplateBinding Panel.Background}" 
       Name="border" 
       SnapsToDevicePixels="True"> 
     <ContentPresenter RecognizesAccessKey="True" 
           Content="{TemplateBinding ContentControl.Content}" 
           ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" 
           ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" 
           Name="contentPresenter" 
           Margin="{TemplateBinding Control.Padding}" 
           HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" 
           VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" 
           SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" 
           Focusable="False" /> 
    </Border> 
    <ControlTemplate.Triggers> 
     <Trigger Property="Button.IsDefaulted" Value="True"> 
      <Setter Property="Border.BorderBrush" TargetName="border"> 
       <Setter.Value> 
        <DynamicResource ResourceKey="{x:Static SystemColors.HighlightBrushKey}" /> 
       </Setter.Value> 
      </Setter> 
     </Trigger> 
     <Trigger Property="UIElement.IsMouseOver" Value="True"> 
      <!--<Setter Property="Panel.Background" TargetName="border"> 
       <Setter.Value> 
        <SolidColorBrush>#FFBEE6FD</SolidColorBrush> 
       </Setter.Value> 
      </Setter>--> 
      <Setter Property="Border.BorderBrush" TargetName="border"> 
       <Setter.Value> 
        <SolidColorBrush>#FF3C7FB1</SolidColorBrush> 
       </Setter.Value> 
      </Setter> 
     </Trigger> 
     <Trigger Property="ButtonBase.IsPressed" Value="True"> 
      <Setter Property="Panel.Background" TargetName="border"> 
       <Setter.Value> 
        <SolidColorBrush>#FFC4E5F6</SolidColorBrush> 
       </Setter.Value> 
      </Setter> 
      <Setter Property="Border.BorderBrush" TargetName="border"> 
       <Setter.Value> 
        <SolidColorBrush>#FF2C628B</SolidColorBrush> 
       </Setter.Value> 
      </Setter> 
     </Trigger> 
     <Trigger Property="ToggleButton.IsChecked" Value="True"> 
      <Setter Property="Panel.Background" TargetName="border"> 
       <Setter.Value> 
        <SolidColorBrush>#FFBCDDEE</SolidColorBrush> 
       </Setter.Value> 
      </Setter> 
      <Setter Property="Border.BorderBrush" TargetName="border"> 
       <Setter.Value> 
        <SolidColorBrush>#FF245A83</SolidColorBrush> 
       </Setter.Value> 
      </Setter> 
     </Trigger> 
     <Trigger Property="UIElement.IsEnabled" Value="False"> 
      <Setter Property="Panel.Background" TargetName="border"> 
       <Setter.Value> 
        <SolidColorBrush>#FFF4F4F4</SolidColorBrush> 
       </Setter.Value> 
      </Setter> 
      <Setter Property="Border.BorderBrush" TargetName="border"> 
       <Setter.Value> 
        <SolidColorBrush>#FFADB2B5</SolidColorBrush> 
       </Setter.Value> 
      </Setter> 
      <Setter Property="TextElement.Foreground" TargetName="contentPresenter"> 
       <Setter.Value> 
        <SolidColorBrush>#FF838383</SolidColorBrush> 
       </Setter.Value> 
      </Setter> 
     </Trigger> 
    </ControlTemplate.Triggers> 
</ControlTemplate> 

如何設置控制模板按鈕?

定義模板下的父面板或用戶控件,並且可以通過靜態資源應用的資源部分地方:

<Grid> 
    <Grid.Resources> 
     <ControlTemplate x:Key="MyTemplate" 
         TargetType="ButtonBase"> 
     ....... 
    </Grid.Resources> 
    <Button Name="register" Template="{StaticResource MyTemplate}"...> 
</Grid> 
+0

如何將控件模板設置爲按鈕 – 2015-02-05 15:12:33

+0

編輯答案。有一張支票。 – 2015-02-05 15:18:22

+1

完美的工作......! – 2015-02-05 15:23:46

2

您必須重寫默認模板才能實現此目的。

<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="50" Height="50" HorizontalContentAlignment="Left" BorderBrush="{x:Null}" Foreground="{x:Null}" Margin="50,0,0,0"> 
      <Button.Style> 
       <Style TargetType="{x:Type Button}"> 
        <Setter Property="Background" Value="Green"/> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate TargetType="{x:Type Button}"> 
           <Border Background="{TemplateBinding Background}"> 
            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 
           </Border> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
        <Style.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="Background" Value="DarkGoldenrod"/> 
         </Trigger> 
        </Style.Triggers> 
       </Style> 
      </Button.Style> 
     </Button> 

希望這會有所幫助。

+0

不透明度與我給出的代碼一起工作,它不會更改背景,邊界刷等。 – 2015-02-05 15:00:57

+0

我剛剛更新了我的答案,請參閱。 – 2015-02-05 15:05:46

4

編輯:作爲羅希特指出in the comments

這億韓元在Windows 8上工作,因爲在PresentationFramework中聲明瞭默認的按鈕模板的一些變化。 ...在Windows 7中默認的按鈕模板沒有該ControlTemplate觸發器。這就是爲什麼你發佈的代碼在Windows7上工作正常,但它不能在Windows 8和更高版本上工作。


Rohit's answer有關的DependencyProperty優先級分別爲起因是正確的,但有固定它不是覆蓋Button的模板的一個更簡單的方法。

如果您查看DependencyProperty Precedence List,你會發現,在<Tag>設置屬性具有優先級高於觸發特性,這就是爲什麼你的按鈕會一直使用您已在<Button>標籤定義的背景。

如果您將Background屬性移至<Style>本身,觸發屬性將起作用,因爲觸發屬性優先於樣式中定義的屬性。

<Button Name="register" Content="Register" ...> 
    <Button.Style> 
     <Style TargetType="Button"> 
      <Setter Property="Background" Value="#FF0090D6" /> 
      <Style.Triggers> 
       <Trigger Property="IsMouseOver" Value="True"> 
        <Setter Property="Background" Value="Red" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Button.Style> 
</Button> 
+0

你有試過嗎?這不起作用,因爲Style設置器與模板觸發器相比具有較低的優先級。 (我只是做了一個交叉檢查,它不起作用)。 – 2015-02-05 15:51:25

+0

是的,我發佈這個答案之前,我測試了它,它工作正常。我使用的是VS2010,Windows 7和.Net 4.0。您是否在使用Button的任何自定義模板?默認優先順序爲''>'TemplatedParent'模板屬性>隱式樣式>樣式觸發器>模板觸發器>樣式設置器>默認值。如果你有一個自定義的模板,那麼你可能需要改變整個Button.Template,否則我不這麼認爲。 – Rachel 2015-02-05 15:55:58

+0

@Rachel在我的情況下不起作用。我正在使用VS 2012旗艦版,Windows 8,.NET 4.5 – 2015-02-05 15:59:01