Style CheckBox text with inlines
-
17-03-2021 - |
Frage
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?
Lösung
The CheckBox
's style must be overriding the TextBlock
's style in its States.
Instead of having a TextBlock
with Run
s as a content of your CheckBox
, try adding a horizontal StackPanel
with TextBlock
s.
If the layout is not rendered as you would expect (for example WordWrap) try editing the style of the ComboBox
.