在XAML中仅使用数据绑定实现TextBlock文本的丰富格式化

3

我正在尝试使用数据绑定格式化推文。我需要做的是根据推文内容类型拆分推文的文本值。

text = "This is a Tweet with a hyperlink http://www.mysite.com"

我需要对文本值中的http://...部分添加一些颜色格式。

关键是,我想仅使用XAML数据绑定来完成此操作。

 <TextBlock x:Name="Tweet1" FontWeight="Bold" Height="207.236" 
    LineHeight="55" TextAlignment="Left" TextWrapping="Wrap" 
    Width="1614.646" Text="{Binding XPath=/statuses/status[2]/text}" 
    FontSize="56" FontFamily="Segoe Book" 
    Foreground="{DynamicResource TextColor-Gray}" />

// 最终需要呈现的效果

<TextBlock x:Name="Tweet1" FontWeight="Bold" ... FontSize="56" FontFamily="Segoe Book">
  <Run Foreground="{DynamicResource TextColor-Gray}" >This is a Tweet with a hyperlink</Run>
<Run Foreground="{DynamicResource TextColor-Pink}" >http://www.mysite.com</Run>
</TextBlock>

这里有一个正则表达式可以用来分割文本值,但我想要严格使用数据绑定。

Regex regUrl = new Regex(@"/http:\/\/\S+/g");

有什么建议吗?
2个回答

2
我正在使用MVVMLight。我的做法是捕获TextBlock的Loaded事件,并将其路由到“转换器”中。
using System.Collections.Generic;
using System.Windows.Documents;
using System.Windows.Controls;

using GalaSoft.MvvmLight.Command;

namespace Converters
{
    public class MyInlineConverter
    {
        public RelayCommand<TextBlock> ConvertTextToInlinesCommand { get; private set; }

        public MyInlineConverter()
        {
            ConvertTextToInlinesCommand = new RelayCommand<TextBlock>(textBlock => convertTextToInlines(textBlock));
        }

        private static void convertTextToInlines(TextBlock textBlock)
        {
            foreach (Run run in textToInlines(textBlock.Text))
                textBlock.Inlines.Add(run);
        }

        private static IEnumerable<Run> textToInlines(string text)
        {
            List<Run> retval = new List<Run>();
            // Perform your conversion here.
            return retval;
        }
    }
}

如果您将此类的一个实例添加到静态资源中,例如:
<converters:TMTInlineConverter x:Key="InlineConverter" />

然后,您可以按照以下方式从TextBlock调用转换器:
                        <TextBlock Text="{Binding MyPath}" TextWrapping="Wrap">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Loaded">
                                    <cmdex:EventToCommand Command="{Binding Source={StaticResource InlineConverter}, Path=ConvertTextToInlinesCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}" />
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </TextBlock>

如果您没有使用MVVMLight,很抱歉。如果您没有使用它,我将把翻译留给读者自己练习。


0

你不能将 Text 绑定并替换为 Run,因为 Text 的类型是 String。相反,你需要绑定 Inlines 并提供一个转换器来解析文本(例如使用正则表达式),并生成适当的 Inlines

<TextBlock Inlines="{Binding XPath=/statuses/status[2]/text, Converter={StaticResource InlineConverter}}"/>

我明白了,谢谢你的确认。我完成后会发布我的转换器。 - discorax
4
您无法绑定到Inlines。"Inlines属性是只读的,不能通过标记设置" 有其他建议吗? - discorax
你的解决方案行不通,但它引导我找到了这个答案,我现在正在尝试解决它。https://dev59.com/dXI-5IYBdhLWcg3wO1rl - discorax

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接