问题
I have a TextBlock
<TextBlock x:Name="TopTextBlock">
Normals words followed by <Bold>Bold words</Bold>
</Textblock>
It displays the text bold.
However, if in my C# code I make any alterations to the string such as
TopTextBlock.Text += " word"
The text will no longer appear bold
回答1:
Add string in TextBlock Inlines instead of setting Text DP :
TopTextBlock.Inlines.Add(" word");
Or in case want to add Bold text :
TopTextBlock.Inlines.Add(new Run(" word") { FontWeight = FontWeights.Bold });
回答2:
The XAML implicitly sets the TextBlock's Inlines
property, whereas in code behind you set the Text
property. You might however add text to the Inlines collection like this:
TopTextBlock.Inlines.Add(new Run(" word"));
or shorter:
TopTextBlock.Inlines.Add(" word");
来源:https://stackoverflow.com/questions/24735058/textblock-loses-boldness-of-bold-text