我的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 :(得分: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>
答案 1 :(得分:0)
这是我使用ControlTemplate的解决方案。在其中使用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"/>
- &GT; ...
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>