zoukankan      html  css  js  c++  java
  • Add hyperlink to textblock wpf

    Add hyperlink to textblock wpf

    Displaying is rather simple, the navigation is another question. XAML goes like this:

    <TextBlock Name="TextBlockWithHyperlink">
        Some text 
        <Hyperlink 
            NavigateUri="http://somesite.com"
            RequestNavigate="Hyperlink_RequestNavigate">
            some site
        </Hyperlink>
        some more text
    </TextBlock>

    And the event handler that launches default browser to navigate to your hyperlink would be:

    private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e) {
        System.Diagnostics.Process.Start(e.Uri.ToString());
    }

    Edit: To do it with the text you've got from database you'll have to parse the text somehow. Once you know the textual parts and hyperlinked parts, you can build textblock contents dynamically in the code:

    TextBlockWithHyperlink.Inlines.Clear();
    TextBlockWithHyperlink.Inlines.Add("Some text ");
    Hyperlink hyperLink = new Hyperlink() {
        NavigateUri = new Uri("http://somesite.com")
    };
    hyperLink.Inlines.Add("some site");
    hyperLink.RequestNavigate += Hyperlink_RequestNavigate;
    TextBlockWithHyperlink.Inlines.Add(hyperLink);
    TextBlockWithHyperlink.Inlines.Add(" Some more text");
  • 相关阅读:
    [JSOI2008]巨额奖金(最小生成树计数)
    [HAOI2008] 糖果传递
    [SCOI2009]生日快乐
    BZOJ2821 作诗
    [HAOI2008]圆上的整点
    POJ1741
    AC自动机
    [JSOI2008]星球大战starwar
    二分图有关证明(感性版)
    初识Pentaho(一)
  • 原文地址:https://www.cnblogs.com/chucklu/p/11437655.html
Copyright © 2011-2022 走看看