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

Geo*_*oué 5 c# xaml windows-10 uwp

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

"<Bold>" + variable + "</Bold>"
Run Code Online (Sandbox Code Playgroud)

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

所以我搜索了如何做,唯一的答案是“创建内联并将其添加到您的 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}}"/>
Run Code Online (Sandbox Code Playgroud)

这是我的转换器:

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;
    }
Run Code Online (Sandbox Code Playgroud)

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

dev*_*xer 5

我一直在 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

xmlns:helpers="clr-namespace:MyProject.Helpers"
Run Code Online (Sandbox Code Playgroud)

要使用格式化程序,只需添加 aTextBlock并声明您的绑定FormattedText(而不是Text),如下所示:

<TextBlock helpers:Formatter.FormattedText="{Binding Content}" />
Run Code Online (Sandbox Code Playgroud)


Geo*_*oué 5

@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);
    }

}
Run Code Online (Sandbox Code Playgroud)

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

xmlns:helpers="using:MyProject.Helpers"
Run Code Online (Sandbox Code Playgroud)

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

<TextBlock  x:Name="textBlock" helpers:TextBlockFormatter.FormattedText="{Binding Content}">
Run Code Online (Sandbox Code Playgroud)