如何在按下按钮时使其背景保持相同? [英] How to keep the background of a button the same while it is pressed?

查看:87
本文介绍了如何在按下按钮时使其背景保持相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
WPF按钮被按下和isEnabled问题

这是我正在使用WPF和XAML开发的应用程序中的代码片段.我想做的是让按钮的背景在用户单击时和在用户未单击时保持相同的颜色.

注意:按钮中有图像.

<Button Name="Button1" Background="#3852A4" Grid.Column="0" Grid.Row="0" Width="35" Height="35" HorizontalAlignment="Left" VerticalAlignment="Top" Click="swapGd1andGd2">
    <Button.Style>
        <Style>
            <Style.Triggers>
                <Trigger Property="Button.IsPressed" Value="True">
                    <Setter Property="Button.Background" Value="#3852A4" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
    <Image Source="Images/112_DownArrowShort_Grey_24x24_72.png" Width="24" Height="24" Stretch="Fill"></Image>
</Button>`

我的问题是,为什么这段代码不能产生我想要的结果?

解决方案

在wpf中,按钮具有覆盖样式值的默认模板.有关如何使用模板更改值的示例,请参见此处./p>

Possible Duplicate:
WPF Button isPressed and isEnabled problem

Here is a snippet of code from an application that I am developing using WPF and XAML. What I am trying to do is get the button's background to stay the same color when it is clicked on by the user as it is when it is not clicked on by the user.

Note: There is an image in the button.

<Button Name="Button1" Background="#3852A4" Grid.Column="0" Grid.Row="0" Width="35" Height="35" HorizontalAlignment="Left" VerticalAlignment="Top" Click="swapGd1andGd2">
    <Button.Style>
        <Style>
            <Style.Triggers>
                <Trigger Property="Button.IsPressed" Value="True">
                    <Setter Property="Button.Background" Value="#3852A4" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
    <Image Source="Images/112_DownArrowShort_Grey_24x24_72.png" Width="24" Height="24" Stretch="Fill"></Image>
</Button>`

My question is, why doesn't this code produce the results that I want?

解决方案

In wpf buttons have a default template that overrides style values. See here for an example of how to change the value using a template.

这篇关于如何在按下按钮时使其背景保持相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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