tags:

views:

314

answers:

2

Hi, I have a WPF Hyperlink that I am able to click and get it's 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.

+3  A: 

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
Stephen Wrighton
Thanks Stephen. I think I see what you mean. I'm going to try the DataContext approach too and see how it works because it seems more elegant.
Max
+2  A: 

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.

Brandon
Works great for me. Thanks!
Max