Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ListBoxItem double-click?

The WPF ListBox doesn't have a DoubleClick event, at least not as far as I can tell. Is there a workaround for this issue that would let me double-click on an item to have an event handler do something with it? Thanks for your help.

like image 555
David Veeneman Avatar asked Mar 30 '10 18:03

David Veeneman


1 Answers

It is possible to bind commands with parameters to ListBoxItems without using code-behind or attached behaviors, simply by using InputBindings with a MouseBinding, as shown before in this answer.

Example ListBox with MouseBinding for LeftDoubleClick:

<ListBox ItemsSource="{Binding MyDataSource}">     <ListBox.ItemTemplate>         <DataTemplate>             <TextBlock Text="{Binding MySourceItemName}">                 <TextBlock.InputBindings>                     <MouseBinding MouseAction="LeftDoubleClick"                                   Command="{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}"                                   CommandParameter="{Binding MySourceItemId}" />                 </TextBlock.InputBindings>             </TextBlock>         </DataTemplate>      </ListBox.ItemTemplate> </ListBox> 

If the command is defined in the same DataContext as the ItemsSource of the ListBox, it can be bound by using the RelativeSource binding as included in the example.

like image 163
marapet Avatar answered Oct 21 '22 02:10

marapet