【问题标题】:How to keep the background of a button the same while it is pressed? [duplicate]如何在按下按钮时保持按钮的背景相同? [复制]
【发布时间】:2026-01-30 12:15:02
【问题描述】:

可能重复:
WPF Button isPressed and isEnabled problem

这是我正在使用 WPF 和 XAML 开发的应用程序的代码的 sn-p。我想要做的是让按钮的背景在用户单击时保持与用户未单击时相同的颜色。

注意:按钮中有图片。

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

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

【问题讨论】:

  • 如果你能告诉我如何更好地格式化这段代码,请告诉我。对*来说相当新。
  • 阅读this
  • 同意。可能合并我的答案?另一个没有提到为什么只设置样式不起作用。

标签: wpf xaml button background click


【解决方案1】:

在 wpf 中,按钮具有覆盖样式值的默认模板。有关如何使用模板更改值的示例,请参阅 here

【讨论】:

  • 谢谢哥们。我已经断断续续地工作了几天,但它并没有像我想要的那样工作。我打算放弃这个功能,它需要很长时间。