WPF 按钮 textwrap 样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/754137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF button textwrap style
提问by mmr
How do I change the default textwrapping style of a button in WPF?
如何更改 WPF 中按钮的默认文本环绕样式?
The obvious solution of:
显而易见的解决方案:
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
<Setter Property="TextWrapping" Value="Wrap"></Setter>
</Style>
doesn't work, because Textwrapping isn't a settable property here, apparently.
不起作用,因为 Textwrapping 在这里不是可设置的属性,显然。
If I try:
如果我尝试:
<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.
Removing the ControlTemplate tag keeps the error.
删除 ControlTemplate 标记会保留错误。
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?
我看到我可以单独为每个按钮设置文本换行,但这太愚蠢了。我怎么能把它作为一种风格呢?神奇的词是什么?
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.
为了将来参考,我在哪里可以找到这些魔法词的列表,这样我就可以自己做这个了?当我试图找出 setter 可以设置哪些属性时,MSDN 条目非常无用。
采纳答案by itowlson
Your second version should work, and does for me, with the caveat that you need to change the TextBlock Text binding:
您的第二个版本应该可以工作,并且对我有用,但需要注意的是您需要更改 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>
Note this completely replaces the button style (i.e. you will need to create your own button chrome if you want it).
请注意,这完全取代了按钮样式(即,如果需要,您将需要创建自己的按钮镶边)。
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.
关于你的第二个问题,所有可写的依赖属性都可以使用 Setter 设置。您无法通过样式在 Button 上设置 TextWrapping 的原因是 Button 没有 TextWrapping 依赖属性(或实际上任何 TextWrapping 属性)。没有“魔法词”,只有依赖属性的名称。
回答by Rob
To expand Eric's answer with an example:-
用一个例子来扩展埃里克的答案:-
<Button Name="btnName" Width="50" Height="40">
<TextBlock Text="Some long text" TextWrapping="Wrap" TextAlignment="Center"/>
</Button>
回答by Eric
I solved this problem by adding a TextBlock
to the button, and using it to display the button text instead of the button's Content
property. Be sure to set the TextBlock
's height property to Auto
, so that it grows in height to accommodate the number of lines of text as it wraps.
我通过向TextBlock
按钮添加 a并使用它来显示按钮文本而不是按钮的Content
属性来解决这个问题。请务必将TextBlock
的 height 属性设置为Auto
,以便它在高度上增加以适应换行时的文本行数。
回答by Danny Beckett
Here's an example of Eric's answer in C# code-behind:
以下是 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
};
回答by Mike Eshva
<Style TargetType="Button">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{TemplateBinding Content}" TextWrapping="Wrap" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>