WPF按钮textwrap风格 [英] WPF button textwrap style

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

问题描述

我要如何改变一个按钮的默认textwrapping风格在WPF?

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

的显而易见的解决方案:

The obvious solution of:

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

我知道我可以为每个单独的按钮textwrapping,但这是pretty愚蠢。我如何能做到这一点作为一个风格?什么是魔法的话?

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?

和以供将来参考,我在哪里可以找到这些神奇的单词的列表,所以我可以做我自己? MSDN的条目是pretty没用的,当我尝试了解哪些属性可以由设定器来设定。

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).

关于你提到的第二个问题,所有可写的依赖属性可以使用二传手进行设置。你无法设置TextWrapping上通过的风格按钮的原因是,巴顿没有一个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天全站免登陆