XAMLでできることはわかっています...
<TextBlock FontSize="18">
This is my text <LineBreak/>
<Run FontSize="24" FontWeight="Bold">My big bold text</Run>
</TextBlock>
質問は、Run をテキスト (文字列) プロパティにプログラムで割り当てるにはどうすればよいですか?
見るTextBlock
と、 ContentProperty がに設定されていることがわかりますInlines
[Localizability(LocalizationCategory.Text), ContentProperty("Inlines")]
public class TextBlock : FrameworkElement, ...
これは、 の開始タグと終了タグの間に追加されたすべてInline
の要素のプロパティに要素を追加することを意味します。Inlines
TextBlock
したがって、Xaml に相当する c# は次のとおりです。
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);