gpt4 book ai didi

WPF 按钮文字环绕样式

转载 作者:行者123 更新时间:2023-12-02 02:13:04 25 4
gpt4 key购买 nike

如何更改 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>

我刚刚从编译器得到了毫无值(value)的响应:

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 条目几乎毫无用处。

最佳答案

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

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

关于WPF 按钮文字环绕样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/754137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com