2

In my UWP (Universal Windows Platform) app I need to create a Hyperlink element with an image in it:

var hyperlink = new Hyperlink();
var container = new InlineUIContainer { Child = new Image() { ... } }; 
hyperlink.Inlines.Add(container);

This Hyperlink gets later added to a TextBlock together with other elements. My problem is, that the last line (Inlines.Add()) throws an exception:

Exception thrown: 'System.ArgumentException' in mscorlib.ni.dll
Additional information: Value does not fall within the expected range.

Does someone know a way around this problem?

Justin XL
  • 38,763
  • 7
  • 88
  • 133
Rico Suter
  • 11,548
  • 6
  • 67
  • 93

1 Answers1

6

I don't think you can do this. A workaround is to use a RichTextBlock with a HyperlinkButton instead.

So try doing this structure in code -

<RichTextBlock>
    <Paragraph>
        <Italic>This is an inline image.</Italic>

        <InlineUIContainer>
            <HyperlinkButton NavigateUri="http://www.nba.com">
                <Image Source="Assets/StoreLogo.png" />
            </HyperlinkButton>
        </InlineUIContainer>

        Yes it is.
    </Paragraph>
</RichTextBlock>
Justin XL
  • 38,763
  • 7
  • 88
  • 133