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?

有帮助吗?

解决方案

The CheckBox's style must be overriding the TextBlock's style in its States.

Instead of having a TextBlock with Runs as a content of your CheckBox, try adding a horizontal StackPanel with TextBlocks.

If the layout is not rendered as you would expect (for example WordWrap) try editing the style of the ComboBox.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top