I am trying to make all the words in a text block that have a URI. Here is an approach:
private static void onTextChanged(DependencyObject dependObj, DependencyPropertyChangedEventArgs e)
{
WrapPanel wrapPanel = ((HyperlinkTextBlock)dependObj).LayoutRoot;
wrapPanel.Children.Clear();
IList<string> words = ((string)e.NewValue).Split(' ').Select(word => word + " ").ToList();
foreach (string word in words)
{
Uri uri;
if (Uri.TryCreate(word, UriKind.Absolute, out uri))
{
wrapPanel.Children.Add(new HyperlinkButton()
{
Content = word,
NavigateUri = uri,
TargetName = "_blank",
Margin = new Thickness(0),
Padding = new Thickness(0),
});
}
else
{
wrapPanel.Children.Add(new TextBlock() { Text = word, TextWrapping = TextWrapping.Wrap });
}
}
}
(I would be fully prepared for a more XAML-oriented / declarative way of doing this, but I'm not sure how I would do it.)
This works great (except it would be nice to use real broken text), except that it HyperlinkButtonlooks funny. It is too large and the text will not be wrapped. It seems that he has a bias, I tried to correct setting Marginand Paddingto 0, but it has not solved the problem.
Any other ideas? Actually, I want HyperlinkTextinstead HyperlinkButton, but I donβt think Silverlight 3 for Windows Phone 7 offers this.