Share via


How to get the particular datacontext values while clicking hyperlink's in the listbox

Question

Tuesday, May 27, 2014 5:39 AM

Hi,

I am using more than one hyperlink's in each list item in listbox. While clicking in any of the hyperlink, i need to get that unique value's and id which was stored in the class list. I tried "Source.Datacontext" for getting this stored unique values, but its not working. Can anyone please help me how to get Value's and id?

All replies (3)

Tuesday, May 27, 2014 12:06 PM âś…Answered | 2 votes

If you want to get a reference to the DataContext of the current ListBoxItem when you click a Hyperlink in the ItemTemplate of the ListBox, you could cast the sender parameter to a Hyperlink in the click event handler of the Hyperlink element and then access its DataContext property:

<ListBox.ItemTemplate>
   <DataTemplate>
    <TextBlock>
     <Hyperlink Click="Hyperlink_OnClick">Text</Hyperlink>
    </TextBlock>
   </DataTemplate>
  </ListBox.ItemTemplate>
  private void Hyperlink_OnClick(object sender, RoutedEventArgs e)
  {
   Hyperlink hpl = sender as Hyperlink;

   YourClass o = hpl.DataContext as YourClass;
   object prop = o.YourProperty;
  }

Note that you have to replace "YourClass" and "YourProperty" in the above sample code with the name of your class and property respectively.


Tuesday, May 27, 2014 9:18 AM

Hi Bharath,

Could you please provide the sample code you are using to better understand your problem.

Well, if you are using bindings, you may try this!

<Hyperlink Command="{Binding YourHyperlinkClickCommand}" CommandParameter="{Binding }" />

Deepak Bhardwaj

Visit my Blog


Friday, May 4, 2018 1:50 PM

Thanks so much for this answer. Was searching for so long