通过绑定在TextBlock中创建超链接 [英] Create Hyperlink in TextBlock via Binding
问题描述
我的问题是从文本内容中找到URL,并通过数据绑定将其转换成可点击的超链接。
My problem is to find the urls from the text content and convert it into the clickable hyperlinks via data binding.
这是我尝试过的
<TextBlock Tag="{Binding message}" x:Name="postDescription" TextWrapping="Wrap"
Grid.Row="3" Grid.ColumnSpan="3" Margin="10,10,10,12" FontSize="16"
TextAlignment="Justify" Foreground="{StaticResource foreGroundWhite}" >
<Run Text="{Binding description, Converter={StaticResource statusFormatter}}" />
</TextBlock>
在代码中,
public class StatusFormatter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return returnTextWithUrl((String)value);
}
public static String returnTextWithUrl(String text)
{
if(text == null) { return null; }
MatchCollection mactches = uriFindRegex.Matches(text);
foreach (Match match in mactches)
{
//Need Help here
HyperlinkButton hyperlink = new HyperlinkButton();
hyperlink.Content = match.Value;
hyperlink.NavigateUri = new Uri(match.Value);
text = text.Replace(match.Value, ??);
}
return text;
}
}
}
输出应该是这个
<TextBlock Tag="{Binding message}" x:Name="postDescription" TextWrapping="Wrap"
Grid.Row="3" Grid.ColumnSpan="3" Margin="10,10,10,12" FontSize="16"
TextAlignment="Justify" Foreground="{StaticResource foreGroundWhite}" >
Click this link -
<Hyperlink NavigateUri="http://www.bing.com">bing</Hyperlink>
- for more info.
</TextBlock>
任何帮助?
推荐答案
为了做你想要的,你将不得不使用你的 TextBlock 的内嵌属性,但是因为它不是一个 DependencyProperty ,它不能成为绑定的目标。我们将不得不扩展您的
To do what you want you will have to use Inlines property of your TextBlock, but as it's not a DependencyProperty, it cannot be a target of binding. We will have to extend your TextBlock class, but as it's sealed we will have to use other class.
允许定义静态类,这将添加适当的内联 - 超链接或运行,具体取决于正则表达式匹配。它可以找到这样的例子:
Lets define static class, which will add apropriate Inline - Hyperlink or Run, depending on Regex match. It can look for example like this:
public static class TextBlockExtension
{
public static string GetFormattedText(DependencyObject obj)
{ return (string)obj.GetValue(FormattedTextProperty); }
public static void SetFormattedText(DependencyObject obj, string value)
{ obj.SetValue(FormattedTextProperty, value); }
public static readonly DependencyProperty FormattedTextProperty =
DependencyProperty.Register("FormattedText", typeof(string), typeof(TextBlockExtension),
new PropertyMetadata(string.Empty, (sender, e) =>
{
string text = e.NewValue as string;
var textBl = sender as TextBlock;
if (textBl != null)
{
textBl.Inlines.Clear();
Regex regx = new Regex(@"(http://[^\s]+)", RegexOptions.IgnoreCase);
var str = regx.Split(text);
for (int i = 0; i < str.Length; i++)
if (i % 2 == 0)
textBl.Inlines.Add(new Run { Text = str[i] });
else
{
Hyperlink link = new Hyperlink { NavigateUri = new Uri(str[i]), Foreground = Application.Current.Resources["PhoneAccentBrush"] as SolidColorBrush };
link.Inlines.Add(new Run { Text = str[i] });
textBl.Inlines.Add(link);
}
}
}));
}
然后在XAML中,我们使用它:
Then in XAML we use it just like this:
<TextBlock local:TextBlockExtension.FormattedText="{Binding MyText}" FontSize="15"/>
将一些文字放到我的财产之后:
And after putting some text to my property:
private void firstBtn_Click(object sender, RoutedEventArgs e)
{
MyText = @"Simple text with http://mywebsite.com link";
}
我可以看到这样的结果:
I can see such a result:
这篇关于通过绑定在TextBlock中创建超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!