如何以编程方式将“运行”分配给文本属性?

我知道在XAML中我们可以做到……

 This is my text  My big bold text  

问题是,如何以编程方式将Run分配给文本(字符串)属性?

如果查看TextBlock您将看到ContentProperty设置为Inlines

 [Localizability(LocalizationCategory.Text), ContentProperty("Inlines")] public class TextBlock : FrameworkElement, ... 

这意味着您将Inline元素添加到属性Inlines以便在TextBlock的开始和结束标记之间添加每个元素。

所以c#等同于你的Xaml

 TextBlock textBlock = new TextBlock(); textBlock.FontSize = 18; textBlock.Inlines.Add("This is my text"); textBlock.Inlines.Add(new LineBreak()); Run run = new Run("My big bold text"); run.FontSize = 24; run.FontWeight = FontWeights.Bold; textBlock.Inlines.Add(run);