wpf - 如何更改按钮的背景色?

标签 wpf wpf-controls

我有3个按钮。我创建了一种样式,用于所有三个按钮来显示MouseOver和Pressed状态。我需要逻辑来表明已选择/单击了哪个按钮。如果单击该按钮,则背景颜色应保持/处于按下状态,而其他两个按钮应重置为背景颜色以恢复为正常状态。下面是我的代码。我想知道是否有可能在XAML中实现全部目标,或者如何在后面的代码中实现目标?谢谢,我提前。

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
x:Class="test3.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">

<Window.Resources>  
    <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" SnapsToDevicePixels="true" Background="#FFFFE640">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="ButtonBackground">
                                            <EasingColorKeyFrame KeyTime="0" Value="#FFC8B432"/>
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="ButtonBackground">
                                            <EasingColorKeyFrame KeyTime="0" Value="#FFBCAA31"/>
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <ContentPresenter RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="0,0,0,4" HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsKeyboardFocused" Value="true">
                        </Trigger>
                        <Trigger Property="ToggleButton.IsChecked" Value="true">
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="#ADADAD"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Storyboard x:Key="OnMouseOneEnter">
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="button_One">
            <EasingColorKeyFrame KeyTime="0" Value="#FFDAC326"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="OnMouseOneLeave">
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="button_One">
            <EasingColorKeyFrame KeyTime="0" Value="#FFFFE640"/>
        </ColorAnimationUsingKeyFrames>
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="button_Two">
            <EasingColorKeyFrame KeyTime="0" Value="#FF85781C"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="OnMoseOneClick">
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="button_One">
            <EasingColorKeyFrame KeyTime="0:0:0.2" Value="#FFDAC326"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>

<Grid>
    <StackPanel x:Name="LayoutRoot">
        <Button x:Name="button_One" HorizontalAlignment="Left" Width="90" Height="90"  Content="One" Style="{DynamicResource ButtonStyle1}" Cursor="Hand" Margin="0,0,0,4" />
        <Button x:Name="button_Two" HorizontalAlignment="Left" Width="90" Height="90" Content="Two" Style="{DynamicResource ButtonStyle1}" Cursor="Hand" Margin="0,0,0,4" />
        <Button x:Name="button_Three" HorizontalAlignment="Left" Width="90" Height="90" Content="Two" Style="{DynamicResource ButtonStyle1}" Cursor="Hand" Margin="0,0,0,4" />
    </StackPanel>   
</Grid>

最佳答案

在我看来,您实际上是在使用单选按钮功能。

http://www.wpftutorial.net/RadioButton.html

为每个单选按钮赋予相同的GroupName,您将获得所需的功能。

关于wpf - 如何更改按钮的背景色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4355355/

相关文章:

c# - 如何在 WPF 中将值从窗口传递到用户控件

wpf - 如何将Textbox的内容保存到文本文件中

c# - 替换 ObservableCollection 时 ListView 不更新

c# - 即使对象不为 null 也会发生 NullReference 异常

c# - WPF:当 DependencyProperty 更改时运行代码

c# - CollectionView 与 WPF(或类似的东西)

wpf - 为什么我的 Grid 的宽度是 NaN?

wpf - 如何将文本框添加到 Gridview 的每个标题列并使用文本框过滤该列?

wpf - 直到我单击其中的控件,弹出窗 Eloquent 会失去焦点并关闭

c# - WPF DataGrid 行颜色基于变量变化