c# wpf 上标(不在 XAML 中)

c# wpf superscript (not in XAML)

我有一个问题要问你。如何仅使用 C# 在文本块中制作上标? 我的目标是动态转换字符串('^' 符号的计数是动态更改的)所以不可能将它写在 XAML 中(或者我不知道该怎么做)。 我尝试了 link - Superscript of superscript in WPF 中的代码。但它不起作用:(

谢谢!

我找到答案了! 可以通过这种方式动态更改 TextBlock:

Run run1 = new Run("This is ");

Run run2 = new Run("bold");
run2.FontWeight = FontWeights.Bold;

Run run3 = new Run(" and ");

Run run4 = new Run("italic");
run4.FontStyle = FontStyles.Italic;

Run run5 = new Run("text.");

Run run6 = new Run("x");

Run run7 = new Run("2");
run7.BaselineAlignment = BaselineAlignment.Subscript;

myTextBlock.Inlines.Add(run1);
myTextBlock.Inlines.Add(run2);
myTextBlock.Inlines.Add(run3);
myTextBlock.Inlines.Add(run4);
myTextBlock.Inlines.Add(run5);
myTextBlock.Inlines.Add(run6);
myTextBlock.Inlines.Add(run7);