【问题标题】:Set the content of a TextBlock设置 TextBlock 的内容
【发布时间】:2014-09-17 16:54:55
【问题描述】:

假设我有一个看起来像这样的TextBlock

<TextBlock TextWrapping="Wrap" >
    Text in <Run Foreground="Red">red</Run> and <Run Foreground="Red">more</Run>
</TextBlock>

效果很好,文本显示为红色,如您所想。但是,在我的真实示例中,此文本:

 Text in <Run Foreground="Red">red</Run> and <Run Foreground="Red">more</Run>

来自服务调用。 (包括标签。)

有没有办法在运行时将此文本提供给TextBlock

【问题讨论】:

  • 这就是为什么我请你解释你将如何标记红色部分。 :)
  • @ErnodeWeerd - 是的,我没有意识到 TextBlock 不能按原样获取字符串。真的,我可以使用任何类型的标记。 (我只是在几分钟前添加了“运行”标签。)我只需要任何方式让服务器标记文本,这样它就可以在客户端显示而无需解析。

标签: wpf textblock


【解决方案1】:

事实上,您必须将所有输入文本转换为Inlines 集合(TextElementCollection 类型)。但是这样做需要自己解析输入文本,这并不容易。那么为什么不使用XamlReader 支持的现有解析器呢。通过使用此类,您可以解析 XAML 代码以获取 TextBlock 的实例,您可以从中获取 Inlines 集合并将其添加为您的实际 TextBlock。代码如下:

var text = "<TextBlock>Text in <Run Foreground=\"Red\">red</Run> and <Run Foreground=\"Red\">more</Run></TextBlock>";
var pc = new System.Windows.Markup.ParserContext();
pc.XmlnsDictionary[""] = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
pc.XmlnsDictionary["x"] = "http://schemas.microsoft.com/winfx/2006/xaml";
var tbl = System.Windows.Markup.XamlReader.Parse(text, pc) as TextBlock;
yourTextBlock.Inlines.Clear();            
yourTextBlock.Inlines.AddRange(tbl.Inlines.ToList());

在被下一个代码使用之前,您的实际输入文本应包含在 &lt;TextBlock&gt;&lt;/TextBlock&gt; 对中。

【讨论】:

  • 效果很好。我把它做成了一个控件,现在可以绑定我的文本了。
【解决方案2】:

我想我会发布我根据金刚的答案制作的控件。 (以防万一它对其他人有用。)

public class FormatBindableTextBlock : TextBlock
{
    private static readonly ParserContext parserContext;

    static FormatBindableTextBlock()
    {
        parserContext = new System.Windows.Markup.ParserContext();
        parserContext.XmlnsDictionary[""] = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
        parserContext.XmlnsDictionary["x"] = "http://schemas.microsoft.com/winfx/2006/xaml";
    }

    public static readonly DependencyProperty FormattedContentProperty =
        DependencyProperty.Register("FormattedContent", typeof(string), typeof(FormatBindableTextBlock), new UIPropertyMetadata(null, OnFormattedContentPropertyChanged));

    public string FormattedContent
    {
        get { return (string)GetValue(FormattedContentProperty); }
        set { SetValue(FormattedContentProperty, value); }
    }

    private static void OnFormattedContentPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        FormatBindableTextBlock textBlock = sender as FormatBindableTextBlock;

        if (textBlock == null)
            return;

        string newFormattedContent = e.NewValue as string;
        if (string.IsNullOrEmpty(newFormattedContent)) 
            newFormattedContent = "";

        newFormattedContent = "<TextBlock>" + newFormattedContent + "</TextBlock>";

        TextBlock tbl = System.Windows.Markup.XamlReader.Parse(newFormattedContent, parserContext) as TextBlock;
        textBlock.Inlines.Clear();
        textBlock.Inlines.AddRange(tbl.Inlines.ToList());

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    • 2013-08-23
    • 1970-01-01
    • 2023-03-04
    • 2011-09-09
    • 1970-01-01
    相关资源
    最近更新 更多