【问题标题】:Can you set the content of a button in wpf with a style?你可以用样式设置 wpf 中按钮的内容吗?
【发布时间】:2009-10-01 14:01:21
【问题描述】:

当我尝试这个时,所有按钮都变成蓝色,但内容没有设置。

<Window.Resources>
    <Style TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Blue"/>       
        <Setter Property="Content" Value="Edit"/>     
    </Style>
</Window.Resources>

有什么想法吗?

编辑:这个例子确实有点过于简单化了。我的问题是在运行时更改样式,其中颜色正在更改,但内容没有更改。我的解决方案不是在 xaml 中初始化内容,而是在代码中。

【问题讨论】:

    标签: wpf button styles


    【解决方案1】:

    请记住,如果您在 Style 中设置了 content 属性,但如果您在 inline xaml 中提供了一些其他内容,它将不起作用,例如,如果您使用

    有时像 Blend 这样的 XAML 编辑器会将 Content="" 如果您只是尝试删除内容,它不会删除属性,您必须检查。

    因此,即使您的 XAML 包含 Content="" 或您的代码将 Content Property 初始化为 null 或空字符串,它也不起作用。

    【讨论】:

      【解决方案2】:

      对我有用(在 kaxaml 中试过)。但是蓝色背景上的黑色文本可能难以阅读。你确定它不在那里?

      【讨论】:

      • 非常确定 :-) .
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 1970-01-01
      • 2011-06-10
      • 1970-01-01
      • 2011-11-07
      相关资源
      最近更新 更多