【问题标题】:How can I set the Content of a WPF Button to have multiple colors?如何将 WPF 按钮的内容设置为具有多种颜色?
【发布时间】:2015-06-27 16:00:44
【问题描述】:
我希望我的 WPF Button 的 Content 使用多种颜色,例如:
<Button Name="MyButton">
<Blue>This is</Blue
<Red>Red</Red>
</Button>
我发现我不能像在TextBlock 中那样使用多个Runs - 实现此效果的正确方法是什么?
【问题讨论】:
标签:
wpf
xaml
button
colors
wpf-controls
【解决方案1】:
您可以将TextBlock 用作Button.Content
<Button Name="MyButton">
<TextBlock>
<Run Foreground="Blue" Text="This is Blue"/>
<Run Foreground="Red" Text=" This is Red"/>
</TextBlock>
</Button>
Button 是 ContentControl 并因此
ContentControl 可以包含任何类型的公共语言运行时对象(例如字符串或 DateTime 对象)或 UIElement 对象(例如 Rectangle 或 Panel)