2016-09-18 237 views
0

我的WPF應用程序未更新單擊按鈕時的按鈕背景,並且鼠標光標位於它上面。背景顏色仍然是淺綠色。其他屬性更改正常。哪裏不對?單擊按鈕時按鈕背景顏色未更新

文件ControlStyles.xaml

... 
<Style TargetType="{x:Type Button}" x:Key="MyButton"> 
     <Setter Property="Foreground" Value="Black"/> 
     <Setter Property="Background" Value="DarkCyan"/>  
     <Setter Property="Margin" Value="5"/> 
     <Setter Property="FontSize" Value="20"/> 
     <Style.Triggers> 
      <Trigger Property="Control.IsMouseOver" Value="true"> 
       <Setter Property="Control.FontStyle" Value="Italic"></Setter> 
       <Setter Property="Control.Foreground" Value="Red"></Setter> 
       <Setter Property="Control.Background" Value="black"></Setter> 
      </Trigger> 
      <Trigger Property="Button.IsPressed" Value="true"> 
       <Setter Property="Control.Foreground" Value="Firebrick"></Setter> 
       <Setter Property="Control.Background" Value="Yellow"></Setter> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
... 

Window.xaml

... 
<Button Name="btn7" Content="7" Grid.Column="0" Grid.Row="3" Style="{StaticResource MyButton}" Click="btn7_Click"/> 
     <Button Name="btn8" Content="8" Grid.Column="1" Grid.Row="3" Style="{StaticResource MyButton}" Click="btn8_Click"/> 
... 

回答

0

看看這個解決您的問題:

<Style TargetType="{x:Type Button}" x:Key="MyButton"> 

    <Setter Property="Foreground" Value="Black"/> 
    <Setter Property="Background" Value="DarkCyan"/>    
    <Setter Property="Margin" Value="5"/> 
    <Setter Property="FontSize" Value="20"/> 

    <Style.Triggers> 
     <Trigger Property="Control.IsMouseOver" Value="true"> 
      <Setter Property="Control.FontStyle" Value="Italic"></Setter> 
      <Trigger.EnterActions> 
       <BeginStoryboard> 
        <Storyboard> 
         <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="Black"/> 
         <ColorAnimation Storyboard.TargetProperty="(Button.Foreground).(SolidColorBrush.Color)" To="Red"/> 
        </Storyboard> 
       </BeginStoryboard> 
      </Trigger.EnterActions> 
      <Trigger.ExitActions> 
       <BeginStoryboard> 
        <Storyboard> 
         <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="DarkCyan"/> 
         <ColorAnimation Storyboard.TargetProperty="(Button.Foreground).(SolidColorBrush.Color)" To="Black"/> 
        </Storyboard> 
       </BeginStoryboard> 
      </Trigger.ExitActions> 
     </Trigger> 
     <Trigger Property="Button.IsPressed" Value="true"> 
      <Trigger.EnterActions> 
       <BeginStoryboard> 
        <Storyboard> 
         <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="Yellow"/> 
         <ColorAnimation Storyboard.TargetProperty="(Button.Foreground).(SolidColorBrush.Color)" To="Firebrick"/> 
        </Storyboard> 
       </BeginStoryboard> 
      </Trigger.EnterActions>      
     </Trigger>     
    </Style.Triggers> 

</Style> 
+0

鼠標懸停:是不是在所有工作(背景顏色是不會改變)。按鈕點擊:以奇怪的方式工作:當鼠標光標離開按鈕時,它會改變顏色 –

0

這是我與控件模板的使用方案。在它內部使用x:Name和TargetName是很重要的。

ControlTemplate.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <ControlTemplate TargetType="{x:Type Button}" x:Key="MyButtonTemplate"> 
     <Border x:Name="tempBorder" CornerRadius="20" Margin="4" BorderThickness="1" BorderBrush="Black" Background="Gold"> 
      <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="8,6,8,6" ContentSource="Content" /> 
     </Border> 
     <ControlTemplate.Triggers> 
      <Trigger Property="IsMouseOver" Value="True"> 
       <Setter TargetName="tempBorder" Property="Background" Value="MediumVioletRed"/> 
      </Trigger> 
      <Trigger Property="IsPressed" Value="True"> 
       <Setter TargetName="tempBorder" Property="Background" Value="LightPink"/> 
      </Trigger> 
     </ControlTemplate.Triggers> 
    </ControlTemplate> 

</ResourceDictionary> 

ConstrolStyles.xaml:

... 
    <Style TargetType="{x:Type Button}" x:Key="MyButton"> 
<!- 
     <Setter Property="Foreground" Value="Black"/> 
     <Setter Property="Background" Value="DarkCyan"/> 

- > ...

Window.xaml:

... 
<Button Name="btnAdd" Content="Add" Grid.Column="4" Grid.Row="1" Style="{StaticResource MyButton}" Template="{StaticResource MyButtonTemplate}" /> 
... 

App.xampl:

<Application x:Class="WpfApplication1.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      > 
    <Application.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="Styles/MyColors.xaml"/> 
       <ResourceDictionary Source="Styles/ControlStyles.xaml"/> 
       <ResourceDictionary Source="Styles/ControlTemplates.xaml"/> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Application.Resources> 
</Application>