2017-05-03 55 views
0

當鼠標懸停時,我想更改我的按鈕背景,邊距和前景,但它僅更改前景。請幫助..WPF觸發器不起作用

<Window x:Class="MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:coupleofbuttons" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Button x:Name="ButtonX" Margin="10,10,0,0" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Height="46" Background="Green" BorderThickness="0,0,0,7"> 
      <Button.Style> 
       <Style TargetType="{x:Type Button}"> 
        <Style.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="Background" Value="DarkGoldenrod"/> 
          <Setter Property="Foreground" Value="Green"/> 
          <Setter Property="Margin" Value="10,20,0,0"/> 
         </Trigger> 
        </Style.Triggers> 
       </Style> 
      </Button.Style> 
     </Button> 
    </Grid> 
</Window> 

回答

1

如果屬性在屬性設置,因爲你已經有MarginBackground進行,即覆蓋任何樣式呢,所以樣式觸發不能改變它。

在樣式中設置這些屬性的默認值,但不在Style.Triggers部分。然後立即取下您現有的Margin和Background屬性。

正如克萊門斯在評論中指出的,這種行爲由Dependency Property Value Precedence的規則解釋。這樣更好:通常你想讓屬性重寫樣式,只有在這種情況下,你纔會希望樣式重寫其他任何東西。

<Button 
    x:Name="ButtonX" 
    Content="Button" 
    HorizontalAlignment="Left" 
    VerticalAlignment="Top" 
    Width="100" 
    Height="46" 
    BorderThickness="0,0,0,7" 
    > 
    <Button.Style> 
     <Style TargetType="{x:Type Button}"> 
      <Setter Property="Margin" Value="10,10,0,0" /> 
      <Setter Property="Background" Value="Green" /> 
      <Style.Triggers> 
       <Trigger Property="IsMouseOver" Value="True"> 
        <Setter Property="Background" Value="DarkGoldenrod"/> 
        <Setter Property="Foreground" Value="Green"/> 
        <Setter Property="Margin" Value="10,20,0,0"/> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Button.Style> 
</Button> 

依稀題外話咆哮

個人而言,作爲軟件的用戶,我覺得很沮喪,有當我點擊他們,這是你的觸發將產生的影響控制移動,當它改變餘量。就在昨天,我向有關該問題的網站發送了反饋。我告訴他們,用他們的頁面感覺就像用棍子戳一個憤怒的浣熊。

但是作爲一名開發人員,這是您的呼叫。說實話,他們的界面比你的界面更加惡劣。它充滿了漩渦紫色的東西。我不得不問他們谷歌分析是否跟蹤了多少觀衆自殺。我懷疑他們會回覆。客戶服務是一種失落的藝術。

+0

非常感謝它的工作,現在我明白「依賴」..謝謝... –

+0

@YassirRabiea太棒了。如果它解決了問題,請將其標記爲正確的答案。 –