3

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.

2 Answers 2

9

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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; 
}

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

1 Comment

How to replace "Click Me" with binding?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.