Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get HyperLink Text from C# in WPF?

Tags:

c#

wpf

I have a WPF Hyperlink which I'm trying to get the text content from.

For example:

<Hyperlink Command="{Binding CustomersCommand}" Name="HLCustomers">
    Customers
</Hyperlink>

This is not possible using the usual manner of accessing a Text property or using VisualTreeHelper to get some child text element, since Hyperlink is not a visual element. I tried to get the text from FirstInline but this also doesn't give me the text.

How would I get the value "Customers" from the Hyperlink element in the above example at runtime?

like image 662
Ejrr1085 Avatar asked Feb 14 '23 20:02

Ejrr1085


1 Answers

If you really need to get the text contained within the Hyperlink, you can dig in to the Inlines property it exposes and get it.

var run = HLCustomers.Inlines.FirstOrDefault() as Run;
string text = run == null ? string.Empty : run.Text;

Note, that this will only work if the first inline in your Hyperlink is indeed a Run. You can finagle with this example for more complex cases.

like image 94
Tejas Sharma Avatar answered Feb 19 '23 19:02

Tejas Sharma