WPF改变对点击按钮背景 [英] WPF changing button background on click

查看:239
本文介绍了WPF改变对点击按钮背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个侧面板一组按钮。我想改变已单击该按钮的背景。我试图做到这一点使用 style.trigger 键,我能想到的唯一的财产就是 IsPressed ,但没有按T帮助那么多,因为它改变了第二个背景(直到按钮被按下[杜])

I have a set of buttons in a side panel. I want to change the background of the button that has been clicked. I have tried to do that using style.trigger and the only property I could think of is IsPressed, but that doesn't help that much since it changes the background for a second (till the button is pressed [duh]).

这是我试过的代码:

<Style.Triggers>
<Trigger Property="IsPressed" Value="True">
    <Setter Property="Background" Value="SlateGray" />
    <Setter Property="Foreground" Value="White"></Setter>
</Trigger>
</Style.Triggers>



另一种方式我能想到的是创造个人风格与每个按钮的datatrigger ,因为我已经与按钮的选择改变,但是,似乎是一个矫枉过正的属性。任何想法我怎么能突出显示已经点击了一个按钮?

Another way I could think of was creating individual style for each button with a datatrigger since I've a property that changes with the selection of the button, but that seems like a overkill. Any idea how can I highlight a button that has been clicked?

推荐答案

当你的条件得到满足,这种触发运行,然后效果消失。为了为好,而不是同时设置看一看这个

This kind of trigger runs when your condition is fulfilled and then the effect disappears. In order to set for good instead of a while take a look at this

<Button Content="Content" Background="Red">
        <Button.Triggers>
            <EventTrigger RoutedEvent="MouseEnter">
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="CadetBlue"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Button.Triggers>
    </Button>



由于IsPressed不是RoutedEvent您可以使用此

Since IsPressed is not a RoutedEvent you can use this

 <Button Content="Content" Background="Red">
        <Button.Style>
            <Style TargetType="Button">
                <Style.Triggers>
                    <Trigger Property="IsPressed" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="CadetBlue"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

这篇关于WPF改变对点击按钮背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆