WPF 按钮 textwrap 样式 [英] WPF button textwrap style

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

问题描述

如何更改 WPF 中按钮的默认文本环绕样式?

How do I change the default textwrapping style of a button in WPF?

显而易见的解决方案:

<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="TextWrapping" Value="Wrap"></Setter>
</Style>

不起作用,因为 Textwrapping 在这里显然不是可设置的属性.

doesn't work, because Textwrapping isn't a settable property here, apparently.

如果我尝试:

<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <TextBlock Text="{Binding}" Foreground="White" FontSize="20" FontFamily="Global User Interface" TextWrapping="Wrap"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我只是从编译器那里得到一个毫无价值的响应:

I just get a worthless response from the compiler:

Error   5   After a 'SetterBaseCollection' is in use (sealed), it cannot be modified.   

删除 ControlTemplate 标记会保留错误.

Removing the ControlTemplate tag keeps the error.

以下尝试产生不同的错误:

The following attempt yields a different error:

    <Setter Property="TextBlock">
        <TextBlock Text="{Binding}" Foreground="White" FontSize="20" FontFamily="Global User Interface" TextWrapping="Wrap"/>
    </Setter>

Error   5   The type 'Setter' does not support direct content.  

我看到我可以单独为每个按钮设置文本换行,但这太愚蠢了.我怎么能把它作为一种风格来做?魔法词是什么?

I see that I can set the textwrapping for each button individually, but that's pretty asinine. How can I do it as a style? What are the magic words?

为了将来参考,我在哪里可以找到这些魔法词的列表,这样我就可以自己做这件事了?当我试图找出 setter 可以设置哪些属性时,MSDN 条目非常无用.

And for future reference, where can I find a list of these magic words, so I can just do this on my own? The MSDN entry is pretty useless when I try to find out about which properties can be set by the setter.

推荐答案

你的第二个版本应该可以工作,对我来说也是如此,但需要注意的是你需要更改 TextBlock 文本绑定:

Your second version should work, and does for me, with the caveat that you need to change the TextBlock Text binding:

<!-- in Window.Resources -->
<Style x:Key="fie" TargetType="Button">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Button}">
        <TextBlock Text="{TemplateBinding Content}" FontSize="20" TextWrapping="Wrap"/>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

<!-- then -->
<Button Style="{StaticResource fie}">verylongcaptiongoeshereandwraps/Button>

请注意,这完全取代了按钮样式(即,如果需要,您将需要创建自己的按钮镶边).

Note this completely replaces the button style (i.e. you will need to create your own button chrome if you want it).

关于你的第二个问题,所有可写的依赖属性都可以使用 Setter 设置.您无法通过样式在 Button 上设置 TextWrapping 的原因是 Button 没有 TextWrapping 依赖属性(或实际上任何 TextWrapping 属性).没有魔法词",只有依赖属性的名称.

Regarding your second question, all writeable dependency properties can be set using a Setter. The reason you were unable to set TextWrapping on a Button via a style is that Button does not have a TextWrapping dependency property (or indeed any TextWrapping property). There are no "magic words," just the names of dependency properties.

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

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