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
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);
Comments
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
Hrvoje Batrnek
How to replace "Click Me" with binding?