locked
Is it possible to make substring of string as a hyperlink? RRS feed

  • Question

  • hello everyone,

    I have content displayed in textblock, is it possible to make substring in that content as hyperlink?

    If possible, how?

    for example feast of Gods in the below sentence,

     painting a feast of the Gods (example illustrated) allowed artists to display their virtuosity

    Thanks in advance.


    • Edited by Anusha V Friday, October 25, 2013 10:26 AM
    Friday, October 25, 2013 10:22 AM

Answers

  • Hi, Anusha

    You can use WinRT XAML toolkit to resolve your problem. See some codes below:

     <HyperlinkButton Name="ExamplesLink" Click="ExamplesLink_Click"
       Extensions:FrameworkElementExtensions.SystemCursor="Hand">
                <TextBlock>
          <Underline>
            <Run Text="Examples"/>
          </Underline>
                </TextBlock>
            </HyperlinkButton>

    Also, you can use the RichTextBlock’s tapped event and its GetPositionFromPoint method:

    <RichTextBlock IsTextSelectionEnabled="False"
                   Margin="30"
                   Width="600"
                   TextAlignment="Justify"
                   FontSize="40"
                   TextWrapping="Wrap"
                   Tapped="UIElement_OnTapped">
                <Paragraph>
                    <Run Text="HyperlinkButton have the similar purpose of enabling a user to launch a specific URI using a separate browser app." />
                    <Run Text=". " />
    
                    <Underline x:Name="LinkToInfiniteSquare"
                       Foreground="#FFFF1BF5">
                        <Run Text="and a HyperlinkButton is a control" />
                     </Underline>
                </Paragraph>
            </RichTextBlock>

    Code-behind:

        private void UIElement_OnTapped(object sender, TappedRoutedEventArgs e)
    {
        var richTB = sender as RichTextBlock;
        var textPointer = richTB.GetPositionFromPoint(e.GetPosition(richTB));
     
        var element = textPointer.Parent as TextElement;
        while (element != null && !(element is Underline))
        {
            if (element.ContentStart != null
                && element != element.ElementStart.Parent)
            {
                element = element.ElementStart.Parent as TextElement;
            }
            else
            {
                element = null;
            }
        }
     
        if (element == null) return;
     
        var underline = element as Underline;
        if (underline.Name == "LinkToInfiniteSquare")
        {
            Launcher.LaunchUriAsync(new Uri("http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.hyperlinkbutton.aspx"));
        }
       
     
    }

    Best Wishes!





    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Anne Jing Tuesday, November 5, 2013 9:42 AM
    Tuesday, October 29, 2013 2:59 AM

All replies

  • if you use 8.1 itss very easy:

         <RichTextBlock>
                <RichTextBlock.Blocks>
                    <Paragraph>
                        text 
                        <Hyperlink NavigateUri="http://www.bing.com">hwufhurehfuer</Hyperlink>
                       eowfouwejfew
                    </Paragraph>
                </RichTextBlock.Blocks>
            </RichTextBlock>


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Friday, October 25, 2013 11:33 AM
  • I am using windows 8,And I am Binding text from sampledatasource, sampledataitem has content property which is of string type in that I want to make a substring as hyperlink.
    Monday, October 28, 2013 4:45 AM
  • Hi, Anusha

    You can use WinRT XAML toolkit to resolve your problem. See some codes below:

     <HyperlinkButton Name="ExamplesLink" Click="ExamplesLink_Click"
       Extensions:FrameworkElementExtensions.SystemCursor="Hand">
                <TextBlock>
          <Underline>
            <Run Text="Examples"/>
          </Underline>
                </TextBlock>
            </HyperlinkButton>

    Also, you can use the RichTextBlock’s tapped event and its GetPositionFromPoint method:

    <RichTextBlock IsTextSelectionEnabled="False"
                   Margin="30"
                   Width="600"
                   TextAlignment="Justify"
                   FontSize="40"
                   TextWrapping="Wrap"
                   Tapped="UIElement_OnTapped">
                <Paragraph>
                    <Run Text="HyperlinkButton have the similar purpose of enabling a user to launch a specific URI using a separate browser app." />
                    <Run Text=". " />
    
                    <Underline x:Name="LinkToInfiniteSquare"
                       Foreground="#FFFF1BF5">
                        <Run Text="and a HyperlinkButton is a control" />
                     </Underline>
                </Paragraph>
            </RichTextBlock>

    Code-behind:

        private void UIElement_OnTapped(object sender, TappedRoutedEventArgs e)
    {
        var richTB = sender as RichTextBlock;
        var textPointer = richTB.GetPositionFromPoint(e.GetPosition(richTB));
     
        var element = textPointer.Parent as TextElement;
        while (element != null && !(element is Underline))
        {
            if (element.ContentStart != null
                && element != element.ElementStart.Parent)
            {
                element = element.ElementStart.Parent as TextElement;
            }
            else
            {
                element = null;
            }
        }
     
        if (element == null) return;
     
        var underline = element as Underline;
        if (underline.Name == "LinkToInfiniteSquare")
        {
            Launcher.LaunchUriAsync(new Uri("http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.hyperlinkbutton.aspx"));
        }
       
     
    }

    Best Wishes!





    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Anne Jing Tuesday, November 5, 2013 9:42 AM
    Tuesday, October 29, 2013 2:59 AM