How to get HyperLink Text from C# in WPF?

后端 未结 3 2095
悲哀的现实
悲哀的现实 2020-12-18 06:19

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

For example:



        
相关标签:
3条回答
  • 2020-12-18 06:57

    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.

    0 讨论(0)
  • 2020-12-18 07:00

    Just put a TextBlock inside and enjoy its binding flexibility .

    If it's still not an option for you - use Run.Text property which is perfectly suitable solution for Hyperlink

    0 讨论(0)
  • 2020-12-18 07:02

    Is adding a text block a problem?

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

    Then you could just reference it as:

    var text = HLCustomersContent.Text;
    

    The .Text property on a WPF Hyperlink object is set to internal, so unless you overrode it and exposed the text property, it is not as easily as accessible as you would might like.

    0 讨论(0)
提交回复
热议问题