Trying to inline a HyperLinkButton in a TextBlock

1.5k Views Asked by At

I'm adding Inlines to a TextBlock. The Run() inlines work. Of course the Hyperlink() is deprecated in Windows 8 (using Windows.UI.Xaml.Documents) for some reason, so I'm trying to encapsulate a HyperlinkButton in an InlineUIContainer using C#. I know the concept is sound, but my code is failing.

if I comment out the line "using Windows.UI.Xaml.Documents;" then I get the two surrounding inlines to show up in my ListView (which uses the method below.) If this line is uncommented, the Inlines.Add for 'link' is throwing. What am I doing wrong?

    public TextBlock enrichPostText(anFullPost post)
    {
        TextBlock text_block = new TextBlock();
        text_block.Inlines.Clear();
        var holdText = new Run();
        holdText.Text = "Test start >> ";
        text_block.Inlines.Add(holdText);

        HyperlinkButton linkButton = new HyperlinkButton();
        linkButton.NavigateUri = new Uri("http://www.cones.net");
        linkButton.Content = "click me";
        linkButton.Name = "_blank";

        InlineUIContainer link = new InlineUIContainer();
        link.Child = linkButton;

        text_block.Inlines.Add(link);

        var holdText2 = new Run();
        holdText2.Text = " << end test.";
        text_block.Inlines.Add(holdText2);

        return (text_block);
    }
2

There are 2 best solutions below

0
On

I just tried changing my RichTextBlock to a TextBlock, and I'm also seeing an ArgumentException get thrown when I try to add the InlineUIContainer. The only solution I know is to use RichTextBlock instead; so far it's been suiting my needs.

0
On

Just found this on MSDN which might be helpful. Apparently TextBlock content model does not support InlineUIContainers so it will throw an exception. http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/96585db1-8ed4-4a12-8d63-b427dc0d543d/