XAML将文本块文本转换为内联

本文关键字:文本 XAML 转换 | 更新日期: 2023-09-27 18:25:51

我想在UWP项目的TextBlock上设置这种文本:

"<Bold>" + variable + "</Bold>"

但将其设置为Text值时不考虑<Bold>标记。

所以我搜索了如何做,唯一的答案是"创建Inlines并将其添加到您的textBlock"。但我不想在我的视图模型上这样做。

因此,我正在寻找一个转换器,用在我的textBlock上设置的内联集合来替换我的文本属性。我找到了一些例子(https://social.msdn.microsoft.com/Forums/en-US/1a1af975-e186-4167-b1c9-cc86afcdd93a/how-to-show-text-in-textblock-as-rich-text-format?forum=wpf),但不适用于通用Windows应用程序(UWP)。

我试过了,但我有一个错误(无法将绑定转换为字符串):

<TextBlock  x:Name="newsArticleSections"
                            Tag="{Binding RelativeSource={RelativeSource Self}, Mode=OneWay, Converter={StaticResource TextToRunConverter}, ConverterParameter={Binding ArticleSections}}"/>

这是我的转换器:

public object Convert(object value, Type targetType, object parameter, string language)
    {
        TextBlock textblock = value as TextBlock;
        textblock.ClearValue(TextBlock.TextProperty);
        Run run = new Run();
        run.Text = (string)parameter;
        textblock.Inlines.Add(run);
        return null;
    }

这只是我探索过的方法,但暂时没有结果。有人有其他想法吗?

XAML将文本块文本转换为内联

@devuxer的回答是个好主意,但仅适用于WPF项目。所以我用它来制作UWP解决方案,它很有效:

创建格式化程序类:

public class TextBlockFormatter
{
    public static readonly DependencyProperty FormattedTextProperty = DependencyProperty.RegisterAttached(
    "FormattedText",
    typeof(string),
    typeof(TextBlockFormatter),
    new PropertyMetadata(null, FormattedTextPropertyChanged));
    public static void SetFormattedText(DependencyObject textBlock, string value)
    {
        textBlock.SetValue(FormattedTextProperty, value);
    }
    public static string GetFormattedText(DependencyObject textBlock)
    {
        return (string)textBlock.GetValue(FormattedTextProperty);
    }
    private static void FormattedTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // Clear current textBlock
        TextBlock textBlock = d as TextBlock;
        textBlock.ClearValue(TextBlock.TextProperty);
        textBlock.Inlines.Clear();
        // Create new formatted text
        string formattedText = (string)e.NewValue ?? string.Empty;
        string @namespace = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
        formattedText = $@"<Span xml:space=""preserve"" xmlns=""{@namespace}"">{formattedText}</Span>";
        // Inject to inlines
        var result = (Span)XamlReader.Load(formattedText);
        textBlock.Inlines.Add(result);
    }
}

并将此引用添加到XAML文件:

xmlns:helpers="using:MyProject.Helpers"

要使用格式化程序,只需添加一个TextBlock并声明您对FormattedText的绑定,如下所示:

<TextBlock  x:Name="textBlock" helpers:TextBlockFormatter.FormattedText="{Binding Content}">

我一直在为WPF项目(而不是UWP)使用以下解决方案,所以我不确定它是否适用于您,但可以尝试一下。

您首先将以下内容放入项目中Helpers文件夹中的类文件中:

public class Formatter
{
    public static readonly DependencyProperty FormattedTextProperty = DependencyProperty.RegisterAttached(
        "FormattedText",
        typeof(string),
        typeof(Formatter),
        new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsMeasure, FormattedTextPropertyChanged));
    public static void SetFormattedText(DependencyObject textBlock, string value)
    {
        textBlock.SetValue(FormattedTextProperty, value);
    }
    public static string GetFormattedText(DependencyObject textBlock)
    {
        return (string)textBlock.GetValue(FormattedTextProperty);
    }
    private static void FormattedTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var textBlock = d as TextBlock;
        if (textBlock == null) return;
        const string @namespace = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
        var formattedText = (string)e.NewValue ?? string.Empty;
        formattedText = $@"<Span xml:space=""preserve"" xmlns=""{@namespace}"">{formattedText}</Span>";
        textBlock.Inlines.Clear();
        using (var xmlReader = XmlReader.Create(new StringReader(formattedText)))
        {
            var result = (Span)XamlReader.Load(xmlReader);
            textBlock.Inlines.Add(result);
        }
    }
}

然后,在XAML文件中,引用命名空间,如下所示:

xmlns:helpers="clr-namespace:MyProject.Helpers"

要使用格式化程序,只需添加一个TextBlock并在FormattedText(而不是Text)上声明绑定,如下所示:

<TextBlock helpers:Formatter.FormattedText="{Binding Content}" />

我使用这样的东西:

XAML

<TextBlock Name="TB" Text="Bold Text " FontWeight="Bold"  />

在控制器处:

TB.Inlines.Add(new Run { Text = "New append text with diferent FontWeigth on the same TextBlock", FontWeight = FontWeights.Normal } );

输出为:

粗体文本在同一个TextBlock 上使用不同字体大小的新追加文本

我希望我的贡献仍然有用,或者可能对其他人提出这个问题有用。我已经从@Geoffrey更新了Formatter类,因此textblock中的特定字符是turning粗体。我有一个人员列表,顶部有一个搜索查询。人员名称中的查询部分正在变为粗体

查询:ic
结果:Rick

public class TextBlockFormatter
{
    const string @namespace = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
    public static readonly DependencyProperty FormattedTextProperty = DependencyProperty.RegisterAttached(
    "FormattedText",
    typeof(string),
    typeof(TextBlockFormatter),
    new PropertyMetadata(null, FormattedTextPropertyChanged));
    public static void SetFormattedText(DependencyObject textBlock, string value)
    {
        textBlock.SetValue(FormattedTextProperty, value);
    }
    public static string GetFormattedText(DependencyObject textBlock)
    {
        return (string)textBlock.GetValue(FormattedTextProperty);
    }
    private static void FormattedTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var textBlock = (TextBlock)d;
        if (textBlock.DataContext == null)
        {
            textBlock.DataContextChanged += TextBlock_DataContextChanged;
            return;
        }
        var query = (string)e.NewValue ?? string.Empty;
        HighlightSearch(textBlock, query);
    }
    private static void HighlightSearch(TextBlock textBlock, string value)
    {
        var name = ((Person)textBlock.DataContext).Name;
        var query = value.ToUpper();
        var indexOfSearch = name.ToUpper().IndexOf(query, 0);
        if (indexOfSearch < 0) return;
        // add normal text first
        var firstText = name.Substring(0, indexOfSearch).Replace("&", "&amp;");
        var first = $@"<Span xml:space=""preserve"" xmlns=""{@namespace}"">{firstText}</Span>";
        var firstResult = (Span)XamlReader.Load(first);
        // add the bold text
        var boldText = name.Substring(indexOfSearch, query.Length).Replace("&", "&amp;");
        var bold = $@"<Bold xml:space=""preserve"" xmlns=""{@namespace}"">{boldText}</Bold>";
        var boldResult = (Bold)XamlReader.Load(bold);
        // add the rest of the text
        var restStartIndex = indexOfSearch + query.Length;
        var restText = name.Substring(restStartIndex, name.Length - restStartIndex).Replace("&", "&amp;");
        var rest = $@"<Span xml:space=""preserve"" xmlns=""{@namespace}"">{restText}</Span>";
        var restResult = (Span)XamlReader.Load(rest);
        // Clear current textBlock
        textBlock.ClearValue(TextBlock.TextProperty);
        textBlock.Inlines.Clear();
        // Inject to inlines
        textBlock.Inlines.Add(firstResult);
        textBlock.Inlines.Add(boldResult);
        textBlock.Inlines.Add(restResult);
    }
    private static void TextBlock_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
    {
        var block = (TextBlock)sender;
        if (block.DataContext == null) return;
        block.DataContextChanged -= TextBlock_DataContextChanged;
        var query = (string)sender.GetValue(FormattedTextProperty);
        HighlightSearch(block, query);
    }
}

在XAML中使用:

<TextBlock Text="{Binding Name}" helpers:TextBlockFormatter.FormattedText="{Binding ElementName=queryTextBox, Path=Text}"  />

我遇到了一些问题,例如,当调用FormattedTextPropertyChanged时,我的datasource没有设置。此外,您还需要注意转义文本。我通过使用Replace函数使自己变得轻松。