C# 仅使用XAML中的数据绑定对文本块中的文本进行丰富格式设置
我正在尝试使用数据绑定格式化Tweet。我需要做的是根据tweet的内容类型来分割tweet的文本值C# 仅使用XAML中的数据绑定对文本块中的文本进行丰富格式设置,c#,wpf,textblock,C#,Wpf,Textblock,我正在尝试使用数据绑定格式化Tweet。我需要做的是根据tweet的内容类型来分割tweet的文本值 text = "This is a Tweet with a hyperlink http://www.mysite.com" 我需要添加一些颜色格式到http://... 文本值的一部分 关键是,我只想使用XAML数据绑定来完成此操作。 <TextBlock x:Name="Tweet1" FontWeight="Bold" Height="207.236" LineHei
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}" />
建议?您不能绑定到
Text
并用Run
s替换,因为Text
属于String
类型。相反,您需要绑定内联线
,并提供一个转换器来解析文本(例如,使用正则表达式)并生成适当的内联线
:
<TextBlock Inlines="{Binding XPath=/statuses/status[2]/text, Converter={StaticResource InlineConverter}}"/>
我正在使用MVVMLight。我所做的是捕获TextBlock的已加载事件,并将其路由到“转换器”
使用System.Collections.Generic;
使用System.Windows.Documents;
使用System.Windows.Controls;
使用GalaSoft.MvvmLight.Command;
名称空间转换器
{
公共类MyInlineConverter
{
public RelayCommand ConvertTextToInlinesCommand{get;private set;}
公共MyInlineConverter()
{
ConvertTextToInlinesCommand=新的RelayCommand(textBlock=>convertTextToInlines(textBlock));
}
私有静态无效转换TextToInLines(TextBlock TextBlock)
{
foreach(在texttoinline(textBlock.Text)中运行)
textBlock.Inlines.Add(运行);
}
私有静态IEnumerable textToInlines(字符串文本)
{
List 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,请道歉。如果你不是,我将把翻译作为练习留给读者。:) 我就是这么想的。谢谢你的确认。我将在构建完转换器后发布它。您不能绑定到内联线。“Inlines属性是只读的,不能由标记设置”还有其他建议吗?您的解决方案不起作用,但它让我找到了这个答案,我现在正在尝试解决这个问题。
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 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>