<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<Button Content="Click Me"
Click="OnClick"
Name="btn"/>
</DataTemplate>
</ListBox.ItemTemplate></ListBox>
I want to get relevant ListBoxItem when I Click the Button.how to do this ?
private void OnClick(object sender, RoutedEventArgs e){// How to get ListBoxItem?}
You can do this using Linq-to-VisualTree as follows:
This finds the first ancestor of the button that is of the desired type.
As an alternative, if you are using databinding, you could inspect the inherited
DataContextof theButtonto see which model item it is bound to. That would be my first preference.