How to get HyperLink text from C # in WPF?

I have WPF Hyperlinkfrom which I am trying to get text content.

For example:

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

This is not possible using the usual method of accessing a property Textor using VisualTreeHelperto get some child text element, since Hyperlinkit is not a visual element. I tried to get text from FirstInline, but that also does not give me text.

How do I get the value "Clients" from an element Hyperlinkin the above example at runtime?

+4
source share
3 answers

, Hyperlink, Inlines, .

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

, , Hyperlink Run. .

+5

TextBlock .

- Run.Text, Hyperlink

+1

?

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

:

var text = HLCustomersContent.Text;

The property .Textin the Hyperlink WPF object is set as internal, so if you do not override it and publish the text property, it is not as simple as you would like.

+1
source

All Articles