C# 在 WPF 中将按钮的文本设置为具有一些粗体字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2277770/
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
Setting a button's text to have some bold characters in WPF
提问by devoured elysium
I'd like to know if it is possible to define as the text of a Buttonin WPF, something like:
a bc
我想知道是否可以Button在 WPF 中定义为 a 的文本,例如: a bc
I've tried setting alt text http://img651.imageshack.us/img651/1838/ctldhrzhy41gbrcch4dpjz4.png
我试过设置 替代文字 http://img651.imageshack.us/img651/1838/ctldhrzhy41gbrcch4dpjz4.png
but that doesn't seem to work.
但这似乎不起作用。
Is it only possible to use the Boldtag with FlowDocuments?
是否只能将Bold标签与FlowDocuments一起使用?
Thanks
谢谢
采纳答案by itowlson
Use a TextBlockto hold the formatted text:
使用 aTextBlock来保存格式化文本:
<Button>
  <TextBlock>Hey <Bold>you</Bold>!!!</TextBlock>
</Button>
Per your comment, if you want to be explicit about the fact that this sets the Contentproperty, you can use XAML property element syntax to do so:
根据您的评论,如果您想明确说明此设置Content属性的事实,您可以使用 XAML 属性元素语法来执行此操作:
<Button>
  <Button.Content>
    <TextBlock>Hey <Bold>you</Bold>!!!</TextBlock>
  </Button.Content>
</Button>
However this is redundant because Buttonhas a ContentPropertyAttributewhich makes the first version exactly equivalent to the second anyway.
然而,这是多余的,因为Button有一个ContentPropertyAttribute使得第一个版本完全等同于第二个版本。
回答by SLaks
Try <Button><TextBlock>a<Bold>b</Bold>c</TextBlock></Button>.
试试<Button><TextBlock>a<Bold>b</Bold>c</TextBlock></Button>。
回答by Steve Danner
This will work.
这将起作用。
<Grid>
   <Button Name="button1" Width="40" Height="40" 
           Content="something" FontWeight="Bold" />
</Grid>
回答by Vlad is not a vampire
The simpliest solution i could think of:
我能想到的最简单的解决方案:
 private void ButtonClick(object sender, RoutedEventArgs e)
 {
     string buttonText = (sender as Button).Content.ToString();
 }

