WPF生成TextBlock的内联 [英] WPF Generate TextBlock Inlines

查看:202
本文介绍了WPF生成TextBlock的内联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 GridView控件和的 GridViewColumn SI 1要产生这样的文字:

I have a GridView and in one of the GridViewColumns i want to generate a text like this:

textBlock.Text = string.Format("{0} is doing {1} .......", a, b);

A B (在视图中的项目的属性)的应不只是重新presented为纯文本,但作为超链接为例。结果
(另外:本格式的文本应取决于项目的类型)

but a and b (Properties of an item in the View) should not just be represented as plain text, but as a Hyperlink for example.
(Also: The format text should depend on the type of the item)

怎样才能在这样的的TextBlock s的文字? (本地化)

How can i generate the TextBlocks text in that way? (for localization)

的问题是更多:?我应该写的东西对我自己还是有框架提供一个简单的方法

推荐答案

最近我遇到了同样的问题。所以我决定实施的TextBlock 附加属性,它采用字符串值键入,然后填充内联收集的飞行。您可以将属性值简单地设置是这样的:

Recently i came across the same problem. So i decided to implement an attached property for TextBlock which takes a value of string type and then populates the Inlines collection on the fly. You can simply set the property value to something like this:

string inlineExpression = "Once i saw a little <bold>bird</bold>, <span>go <bold>hop, hop, hop</bold></span>.";
InlineExpression.SetInlineExpression(myTextBlock1, inlineExpression);

的样式也支持:

string inlineExpression = "<run style="Theme.GrayText">Once i saw a little bird, go hop, hop, hop.</run>";
InlineExpression.SetInlineExpression(myTextBlock1, inlineExpression);

您还可以在一个标准的方式使用XAML这个附加属性。

You can also use this attached property in XAML in a standard way.

下面是类的code暴露出这个属性:

Here is the code of the class which exposes this property:

public class InlineExpression
{
    public static readonly DependencyProperty InlineExpressionProperty = DependencyProperty.RegisterAttached(
        "InlineExpression", typeof(string), typeof(TextBlock), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsMeasure));

    public static void SetInlineExpression(TextBlock textBlock, string value)
    {
        textBlock.SetValue(InlineExpressionProperty, value);

        textBlock.Inlines.Clear();

        if (string.IsNullOrEmpty(value))
            return;

        var descriptions = GetInlineDescriptions(value);
        if (descriptions.Length == 0)
            return;

        var inlines = GetInlines(textBlock, descriptions);
        if (inlines.Length == 0)
            return;

        textBlock.Inlines.AddRange(inlines);
    }

    public static string GetInlineExpression(TextBlock textBlock)
    {
        return (string)textBlock.GetValue(InlineExpressionProperty);
    }

    enum InlineType
    {
        Run,
        LineBreak,
        Span,
        Bold,
        Italic,
        Hyperlink,
        Underline
    }

    class InlineDescription
    {
        public InlineType Type { get; set; }
        public string Text { get; set; }
        public InlineDescription[] Inlines { get; set; }
        public string StyleName { get; set; }
    }

    private static Inline[] GetInlines(FrameworkElement element, IEnumerable<InlineDescription> inlineDescriptions)
    {
        var inlines = new List<Inline>();
        foreach (var description in inlineDescriptions)
        {
            var inline = GetInline(element, description);
            if (inline != null)
                inlines.Add(inline);
        }

        return inlines.ToArray();
    }

    private static Inline GetInline(FrameworkElement element, InlineDescription description)
    {
        Style style = null;
        if (!string.IsNullOrEmpty(description.StyleName))
        {
            style = element.FindResource(description.StyleName) as Style;
            if (style == null)
                throw new InvalidOperationException("The style '" + description.StyleName + "' cannot be found");
        }

        Inline inline = null;
        switch (description.Type)
        {
            case InlineType.Run:
                var run = new Run(description.Text);
                inline = run;
                break;
            case InlineType.LineBreak:
                var lineBreak = new LineBreak();
                inline = lineBreak;
                break;
            case InlineType.Span:
                var span = new Span();
                inline = span;
                break;
            case InlineType.Bold:
                var bold = new Bold();
                inline = bold;
                break;
            case InlineType.Italic:
                var italic = new Italic();
                inline = italic;
                break;
            case InlineType.Hyperlink:
                var hyperlink = new Hyperlink();
                inline = hyperlink;
                break;
            case InlineType.Underline:
                var underline = new Underline();
                inline = underline;
                break;
        }

        if (inline != null)
        {
            var span = inline as Span;
            if (span != null)
            {
                var childInlines = new List<Inline>();
                foreach (var inlineDescription in description.Inlines)
                {
                    var childInline = GetInline(element, inlineDescription);
                    if (childInline == null)
                        continue;

                    childInlines.Add(childInline);
                }

                span.Inlines.AddRange(childInlines);
            }

            if (style != null)
                inline.Style = style;
        }

        return inline;
    }

    private static InlineDescription[] GetInlineDescriptions(string inlineExpression)
    {
        if (inlineExpression == null)
            return new InlineDescription[0];

        inlineExpression = inlineExpression.Trim();
        if (inlineExpression.Length == 0)
            return new InlineDescription[0];

        inlineExpression = inlineExpression.Insert(0, @"<root>");
        inlineExpression = inlineExpression.Insert(inlineExpression.Length, @"</root>");

        var xmlTextReader = new XmlTextReader(new StringReader(inlineExpression));
        var xmlDocument = new XmlDocument();
        xmlDocument.Load(xmlTextReader);

        var rootElement = xmlDocument.DocumentElement;
        if (rootElement == null)
            return new InlineDescription[0];

        var inlineDescriptions = new List<InlineDescription>();

        foreach (XmlNode childNode in rootElement.ChildNodes)
        {
            var description = GetInlineDescription(childNode);
            if (description == null)
                continue;

            inlineDescriptions.Add(description);
        }

        return inlineDescriptions.ToArray();
    }

    private static InlineDescription GetInlineDescription(XmlNode node)
    {
        var element = node as XmlElement;
        if (element != null)
            return GetInlineDescription(element);
        var text = node as XmlText;
        if (text != null)
            return GetInlineDescription(text);
        return null;
    }

    private static InlineDescription GetInlineDescription(XmlElement element)
    {
        InlineType type;
        var elementName = element.Name.ToLower();
        switch (elementName)
        {
            case "run":
                type = InlineType.Run;
                break;
            case "linebreak":
                type = InlineType.LineBreak;
                break;
            case "span":
                type = InlineType.Span;
                break;
            case "bold":
                type = InlineType.Bold;
                break;
            case "italic":
                type = InlineType.Italic;
                break;
            case "hyperlink":
                type = InlineType.Hyperlink;
                break;
            case "underline":
                type = InlineType.Underline;
                break;
            default:
                return null;
        }

        string styleName = null;
        var attribute = element.GetAttributeNode("style");
        if (attribute != null)
            styleName = attribute.Value;

        string text = null;
        var childDescriptions = new List<InlineDescription>();

        if (type == InlineType.Run || type == InlineType.LineBreak)
        {
            text = element.InnerText;
        }
        else
        {
            foreach (XmlNode childNode in element.ChildNodes)
            {
                var childDescription = GetInlineDescription(childNode);
                if (childDescription == null)
                    continue;

                childDescriptions.Add(childDescription);
            }
        }

        var inlineDescription = new InlineDescription
                                        {
                                            Type = type,
                                            StyleName = styleName,
                                            Text = text,
                                            Inlines = childDescriptions.ToArray()
                                        };

        return inlineDescription;
    }

    private static InlineDescription GetInlineDescription(XmlText text)
    {
        var value = text.Value;
        if (string.IsNullOrEmpty(value))
            return null;

        var inlineDescription = new InlineDescription
                                        {
                                            Type = InlineType.Run,
                                            Text = value
                                        };
        return inlineDescription;
    }
}

这篇关于WPF生成TextBlock的内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆