【问题标题】:WPF how to restore Button background colorWPF如何恢复Button背景颜色
【发布时间】:2011-07-02 16:13:29
【问题描述】:

这个问题感觉很简单,但我就是找不到答案:

如何将 Button 改回其默认值?我的 VS2010 开始给我一个奇怪颜色的按钮,我必须手动设置按钮看起来像它的默认自我。

我试过了:

btn.Background = null; // only make it transparent, not default background

有人吗?

【问题讨论】:

    标签: wpf button


    【解决方案1】:

    使用ClearValue-方法恢复默认。

    btn.ClearValue(Button.BackgroundProperty);
    

    btn.ClearValue(Control.BackgroundProperty);
    

    这会设置按钮的背景属性。但是,如果您更改了按钮模板,这将无济于事。在这种情况下,请查找 Button.Template-property 的显式声明或设置 Button.Template-property 的样式。如果有类似

    的内容,请特别查看您的 App.xaml
    <style TargetType="Button">
      ...
    </style>
    

    <style TargetType="{x:Type Button}">
      ...
    </style>
    

    【讨论】:

    • 如何在 MVVM 中做到这一点?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 2023-03-30
    相关资源
    最近更新 更多