Finding which part of the ListBox ItemTemplate was clicked twice

I have a data bound ListBox that uses a custom ItemTemplate to display my stuff. ItemTemplate has a 4x2 grid that contains different items. Now I want my users to be able to get different data depending on which subtype they double click on - so if they double click on the image at row 0, column 0, they get one window. If they double click on the text box in row 1, column 2, they get a different type of window with different information.

And, of course, the information will be unique for each row in the ListBox.

Is there any easy way to get information about which row / column of the grid the user has double-clicked?

The ItemTemplate is defined in an external resource dictionary, so I don't see a way to bind it to the events themselves.

Do I need to create a custom control or can it be done without it?

Thanks in advance!

0


source to share


1 answer


WPF introduces RoutedEvents . In your case, you can simply add a MouseDoubleClick event handler to the ListBox.

<ListBox ItemsSource="{Binding Path=myData}"
         ItemTemplate="{StaticResource template}"
         MouseDoubleClick="ListBox_MouseDoubleClick">
</ListBox>

      



In the code behind you will get a parameter of type MouseButtonEventArgs that contains information about the original source.

MessageBox.Show(e.OriginalSource.ToString());

      

+1


source







All Articles