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.
It is possible to bind commands with parameters to ListBoxItem
s 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With