【问题标题】:Button background color is not updating when button clicked单击按钮时按钮背景颜色未更新
【发布时间】:2017-01-26 01:11:42
【问题描述】:

我的 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"/>
...

【问题讨论】:

    标签: wpf styles


    【解决方案1】:

    看看这是否解决了你的问题:

    <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>
    

    【讨论】:

    • 鼠标悬停:根本不工作(背景颜色没有改变)。按钮单击:以奇怪的方式工作:当鼠标光标离开按钮时它会改变颜色
    【解决方案2】:

    这是我使用 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>
    

    ControlStyles.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>
    

    【讨论】:

      猜你喜欢
      • 2014-10-09
      • 2017-08-02
      • 2013-06-02
      • 1970-01-01
      • 2014-10-10
      • 2019-04-14
      • 1970-01-01
      • 2014-10-07
      相关资源
      最近更新 更多