简体   繁体   中英

Making URLs on a textblock clickable

I'm programming a little Twitter Client just for fun. I have the tweet's text on a TextBlock and I want to make the URLs clickable.

I know that I have to parse the URLs with a regexp but... how I put text plus link on the TextBlock ?

I can't have a string like: Hello check my blog at <Hyperlink>http​://myblogurl.com</Hyperlink> because the TextBlock doesn't parse the tags.

Then, how I can have a TextBlock that maybe has a link or maybe not?

Thank you.

<RichTextBox  IsDocumentEnabled="True">
        <FlowDocument>
          <Paragraph>
          This is a richTextBox. And this is a <Hyperlink NavigateUri="http://www.microsoft.com">Hyperlink</Hyperlink>.
          </Paragraph>
        </FlowDocument>
  </RichTextBox>

MSDN discussion

Rather than use a TextBlock, take a look at using the WPF version of the RichTextBox. It's a very flexible little critter.

Something like...

<TextBlock>
    <Hyperlink Name="btnOpen" Click="btnOpen_Click">
        <TextBlock Text="Click to Open" />
    </Hyperlink>
</TextBlock>

您可以在后面的代码中解析字符串,并建立内容控件的集合,将文本块更改为自动换行面板,然后将面板的子级设置为您创建的集合。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM