如何从wpf中的方法调用返回格式化文本

时间:2011-10-17 12:27:10

标签: c# wpf richtextbox string-formatting

我正在开发一个插件接口,我想有一个方法以WPF控件只能显示插件结果的方式返回格式化文本。返回该文本的最佳方法是什么?

这样我只需要将插件结果分配给WPF文本控件(可能是RichTextBox或类似的东西)。

我想支持粗体,下划线等......

编辑:总结一下,我正在搜索一个字符串“格式”,它允许第三方编码器返回一个带编码格式的简单字符串,所以我只需要这样做:

 myRichTextBox.Text = (IPlugin)3rdPartyPlugin.ExecutePlugin();

文字显示格式化。

2 个答案:

答案 0 :(得分:2)

返回格式化文本的方法背后有各种代码......

1] As Text

WPF Flowdocument and Inlines

2]作为图形呈现的文本......

在任何自定义控件的覆盖OnRender()方法中,您可以图形化地将格式化文本绘制为绘图......

    protected override void OnRender(DrawingContext drawingContext)
    {
        this.formattedToolTip = new FormattedText(
                (string)this.TextProperty,
                System.Globalization.CultureInfo.CurrentCulture,
                FlowDirection.LeftToRight,
                new Typeface(
                     new FontFamily("Arial"),
                     FontStyles.Normal,
                     FontWeights.Bold,
                     FontStretches.Normal),
                11,
                new SolidColorBrush(Colors.Black));

        drawingContext.DrawText(
                this.formattedToolTip,
                new Point(10, 10)); //// Margin of 10 pixels from top and left.
    }

答案 1 :(得分:0)

我只想去String。在您喜欢IIRC的任何编码中,RTF文本都可以很好地表示为XML。

有些东西告诉我你实际上并没有问“如何返回文本”,而是如何生成它?如果是这样,请重新提出问题。

相关问题