Build hyperlink in wpf c #

how can i insert a TextBlock control into a hyperlink in c # encoding. similar to <TextBlock> <Hyperlink>a</Hyperlink></textblock

in C #. I cannot find the content property in the hyperlink. Thanks in advance.

+2


a source to share


3 answers


Try using Inlines to add hyperlink to TextBlock and add text to HyperLink



TextBlock textBlock = new TextBlock();
Hyperlink link = new Hyperlink();
link.Inlines.Add("Click me");
textBlock.Inlines.Add(link);

      

+6


a source


Use a HyperlinkButton, not a hyperlink (which is meant to be used in a FlowDocument that has Inlines inside)

HyperlinkButton has a Content property as a content property.



So:

TextBlock tb = new TextBlock();
// set tb properties

HyperlinkButton hlb = new HyperlinkButton();
// set hlb properties
hlp.Content = tb;

      

0


a source


If you want to use TextBlock. I am using what I ran into and works great for me.

XAML:

< TextBlock >
    < Hyperlink NavigateUri="http://yoursite.com" RequestNavigate="Hyperlink_RequestNavigate" >
        Click Me
    < /Hyperlink >
< /TextBlock >

      

Code for:

private void Hyperlink_RequestNavigate(object sender, 
    System.Windows.Navigation.RequestNavigateEventArgs e) {
    System.Diagnostics.Process.Start(
        new System.Diagnostics.ProcessStartInfo(e.Uri.AbsoluteUri)
     );
    e.Handled = true;
} 

      

===============================

I've seen this a lot as a solution, but was getting an error in Process.Start. Have done some reading and figured out what is the best for web applications. Whether this solution or not the solution above solved my hyperlink problem.

protected void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)  { 
    Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri)); 
    e.Handled = true; 
}

      

===============================

0


a source







All Articles