fad
fad

Reputation: 33

construct hyperlink in wpf C#

how can i insert a TextBlock control in a Hyperlink in c# coding. similar to <TextBlock> <Hyperlink>a</Hyperlink></textblock in C#. i'm unable to find content property in Hyperlink. Thanks in advance.

Upvotes: 3

Views: 6399

Answers (2)

Albert
Albert

Reputation: 1

If you want to use a TextBlock. I use something I ran across and works great for me.

XAML:

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

Code Behind:

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 have seen this alot as the solution but was getting an error at Process.Start. Did some more reading and found out this is best for Web Apps. Whether it is or is not the solution posted above solved my hyperlink issue.

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

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

Upvotes: 0

Hun1Ahpu
Hun1Ahpu

Reputation: 3355

Try to use Inlines to add Hyperlink to TextBlock and to add text to HyperLink

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

Upvotes: 9

Related Questions