Tuesday, March 1, 2011

Finding which part of an ListBox ItemTemplate was double clicked

Hi,

I have a data bound ListBox that uses a custom ItemTemplate to display my stuff. The ItemTemplate has a 4x2 grid that contains different elements. Now I want my users to be able to get at different pieces of data, depending on the subitem they double click - so if they double click the image in row 0, column 0, they get one window. If they double click the textblock 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 simple way to get information about which row/column of the grid that the user double clicked?

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

Do I need to make a custom control, or is this doable without it?

Thanks in advance!

From stackoverflow
  • WPF introduces RoutedEvents. In your case you can simply add a MouseDoubleClick event handler to you ListBox.

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

    On the code behind you will get a parameter of the type MouseButtonEventArgs that contains informations about the orginal source.

    MessageBox.Show(e.OriginalSource.ToString());
    
    Rune Jacobsen : Thanks, that did it - a bit of work but works like a charm! :)

0 comments:

Post a Comment