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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With