Textblock loses boldness of bold text

浪子不回头ぞ 提交于 2019-12-13 04:18:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!