带有内联样式的复选框文本
我正在尝试在包含 TextBlocks 和 TextBlocks 的页面上设置文本样式作为 CheckBoxes 的内容
我想做这样的事情(其中 textBlock 是 CheckBox 中包含的 TextBlock - 或从 System.Windows.Controls.Control 继承的任何内容)
textBlock.Inlines.Add("unstyledText");
textBlock.Inlines.Add(new Run { Text = "yellowBoldText ", Foreground = new SolidColorBrush(Colors.Yellow), FontWeight = FontWeights.Bold });
textBlock.Inlines.Add("unstyledText");
这适用于不在复选框内的文本块。如果它们位于 CheckBox 内,则 TextBlock 的 Text 属性将会更改(即 textBlock.Text == "unstyledText YellowBoldText unstyledText"
返回 true),但 CheckBox 中的文本在视觉上不会发生变化。
是否有办法让 CheckBox 内的 TextBlock 的行为与其他 TextBlock 的行为相同?
I am trying to style text on a page that contains TextBlocks and TextBlocks as the content of CheckBoxes
I want to do something like this (where textBlock is the TextBlock contained within the CheckBox - or anything that inherits from System.Windows.Controls.Control)
textBlock.Inlines.Add("unstyledText");
textBlock.Inlines.Add(new Run { Text = "yellowBoldText ", Foreground = new SolidColorBrush(Colors.Yellow), FontWeight = FontWeights.Bold });
textBlock.Inlines.Add("unstyledText");
This works on the TextBlocks that are not inside a CheckBox. If they are inside a CheckBox, the TextBlock's Text property will change (ie. textBlock.Text == "unstyledText yellowBoldText unstyledText"
returns true), but the text in the CheckBox will not visually change.
Is there anyway to get the TextBlocks inside a CheckBox to behave the same way as the other TextBlocks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CheckBox
的样式必须覆盖TextBlock
在其状态中的样式。不要使用带有
Run
的TextBlock
作为CheckBox
的内容,而是尝试添加带有StackPanel
的水平StackPanel
代码>文本块。如果布局未按您预期的方式呈现(例如 WordWrap),请尝试编辑
ComboBox
的样式。The
CheckBox
's style must be overriding theTextBlock
's style in its States.Instead of having a
TextBlock
withRun
s as a content of yourCheckBox
, try adding a horizontalStackPanel
withTextBlock
s.If the layout is not rendered as you would expect (for example WordWrap) try editing the style of the
ComboBox
.