【问题标题】:WPF button textwrap styleWPF 按钮文本换行样式
【发布时间】:2010-10-19 17:48:27
【问题描述】:

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

显而易见的解决方案:

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

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

如果我尝试:

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

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

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

删除 ControlTemplate 标记会保留错误。

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

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

我看到我可以为每个按钮单独设置文本换行,但这很愚蠢。我怎样才能把它作为一种风格?什么是神奇的词?

为了将来参考,我在哪里可以找到这些神奇单词的列表,这样我就可以自己做这件事了?当我试图找出 setter 可以设置哪些属性时,MSDN 条目几乎没有用。

【问题讨论】:

    标签: wpf button styles default word-wrap


    【解决方案1】:

    用一个例子来扩展埃里克的答案:-

    <Button Name="btnName" Width="50" Height="40">
       <TextBlock Text="Some long text" TextWrapping="Wrap" TextAlignment="Center"/>
    </Button>
    

    【讨论】:

    • 我没有在按钮上设置宽度和高度,而是在文本上使用了 MaxWidth - 限制了大小并且包裹得很好!
    • 如果您希望能够指定访问密钥 (Text="_Some long text"),请将 TextBlock 更改为 AccessText
    【解决方案2】:

    我通过向按钮添加TextBlock 解决了这个问题,并使用它来显示按钮文本而不是按钮的Content 属性。请务必将TextBlock 的高度属性设置为Auto,以便它的高度增加以适应其换行时的文本行数。

    【讨论】:

    • 这绝对比公认的答案更容易/更干净,但他的方式也确实有效。
    • 有关所需代码的示例,请参见以下答案。
    • 非常感谢您提到 height=Auto....
    【解决方案3】:

    您的第二个版本应该可以工作,并且对我有用,但需要注意的是您需要更改 TextBlock 文本绑定:

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

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

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

    【讨论】:

    • 不,有魔法词。在这种情况下,神奇的词是“模板绑定内容”。谢谢你告诉我。
    【解决方案4】:
    <Style TargetType="Button">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="{TemplateBinding Content}" TextWrapping="Wrap" />
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    【讨论】:

    • 这是人们应该选择的答案,如果他们希望在不破坏 chrome 的情况下普遍使所有按钮环绕文本。
    【解决方案5】:

    以下是 Eric 在 C# 代码隐藏中的回答示例:

    var MyButton = new Button();
    
    MyButton.Content = new TextBlock() {
        FontSize        = 25,
        Text            = "Hello world, I'm a pretty long button!",
        TextAlignment   = TextAlignment.Center,
        TextWrapping    = TextWrapping.Wrap
    };
    

    【讨论】:

      【解决方案6】:

      用@fadden 的评论扩展@Rob 的答案:

      <Button Width="50" Height="40">
         <AccessText Text="_Some long text" TextWrapping="Wrap" TextAlignment="Center"/>
      </Button>
      

      TextBlock 控件不支持键盘热键 (_)。

      【讨论】:

        猜你喜欢
        • 2012-04-23
        • 2015-02-04
        • 2017-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-11
        • 1970-01-01
        相关资源
        最近更新 更多