Max
Max

Reputation: 7119

WPF Hyperlink Child

I have a WPF Hyperlink that I am able to click and get its NavigateUri property just fine. However, I want to be able to bundle some additional information with the Hyperlink so I can process it in my event handler. This is how it looks 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 the data inside the two textblocks that have visibility = "Collapsed" (data1 and data2). I liken this to "hidden" data in an HTML form.

I tried messing with the "Inlines" property of Hyperlink but that's not working, and since this is inside a DataTemplate I can't access data1 and data2 by name in my code.

Any ideas?

Thanks.

Upvotes: 1

Views: 1387

Answers (2)

Brandon
Brandon

Reputation: 69983

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.

Upvotes: 2

Stephen Wrighton
Stephen Wrighton

Reputation: 37819

creating textblocks to hold that data is somewhat... overkill. I'd go with one of these two options:

  1. use databinding, to place a specific object into the hyperlink, then to get it back, all you need to do, is access the DataContext of the hyperlink,and it will provide you the class which holds data1 and data2
  2. attach the object which populates data1 and data2 into the Hyperlink's TAG attribute

Upvotes: 4

Related Questions