WPF Hyperlink

I have WPF Hyperlink

that I can click and get its property NavigateUri

just fine. However, I want to be able to associate some additional information with Hyperlink

, so I can handle it in an event handler. This is what it looks like right now:

<TextBlock Grid.Row="0">
    <Hyperlink ToolTip="{Binding Path=Contact.ToolTipPersonalEmail}" 
           Name="ContactHyperlink" Foreground="#FF333333" 
           RequestNavigate="HandleContactEmailClicked" 
           NavigateUri="{Binding Path=Contact.Email}"
           >
        <TextBlock Text="{Binding Path=Contact.Fullname}" Width="Auto"
            HorizontalAlignment="Stretch"
            TextTrimming="CharacterEllipsis"/>
        <TextBlock Text="{Binding Path=Data1}" Name="data1"  Visibility="Collapsed" />
        <TextBlock Text="{Binding Path=Data2}" Name="data2"  Visibility="Collapsed" />  
    </Hyperlink>

</TextBlock>

      

Basically, in my event handler, I want to be able to access data inside two text blocks that have visibility = "Collapsed" (data1 and data2). I compare this to "hidden" data in an HTML form.

I tried messing up the "Inlines" property Hyperlink

, but that doesn't work, and since it is inside the DataTemplate, I cannot access data1 and data2 by name in my code.

Any ideas?

Thanks.

0


a source to share


2 answers


In your event handler, you can do something like this:

ContentPresenter presenter = (ContentPresenter)sender.TemplatedParent;
DataTemplate template = presenter.ContentTemplate;
TextBlock textBlock = (TextBlock)template.FindName("data1", presenter);

      



Probably not the prettiest way, but it works for me.

+2


a source


creating text blocks to store this data is somewhat ... overkill. I would use one of these two options:



  • use data binding to place a specific object in the hyperlink, then return, all you have to do is access the DataContext for the hyperlink and it will give you a class that contains data1 and data2
  • attach an object that populates data1 and data2 into the TAG attribute of the hyperlink
+3


a source







All Articles