在TextBlock中插入HTML标记

时间:2014-07-24 09:51:42

标签: c# html wpf xaml

我有小问题

这很好用,我看到了标签:

 <TextBlock TextWrapping="Wrap" Name="RTBOX">
           Hello <Bold>my</Bold> faithful <LineBreak/> sdfsdf <Underline>computer</Underline>.<Italic>You rock!</Italic>
  </TextBlock>

但事实并非如此,我将所有标签视为文字:

RTBOX.Text = " Hello <Bold>my</Bold> faithful <LineBreak/> sdfsdf <Underline>computer</Underline>.<Italic>You rock!</Italic>";

1 个答案:

答案 0 :(得分:1)

在c#中,您可以使用内嵌运行

来实现
        RTBOX.Inlines.Add("hello ");
        Run run = new Run("my ");         
        run.FontWeight = FontWeights.Bold;
        Run run1 = new Run("faithfull\n ");
        Run run2 = new Run("sdsf ");
        Run run3 = new Run("Computer");           
        run3.TextDecorations = TextDecorations.Underline;

        Run run4 = new Run(" You rock !");
        run4.FontStyle = FontStyles.Italic;

        RTBOX.Inlines.Add(run);
        RTBOX.Inlines.Add(run1);
        RTBOX.Inlines.Add(run2);
        RTBOX.Inlines.Add(run3);
        RTBOX.Inlines.Add(run4);

<强>结果

enter image description here