MouseBinding/LeftDoubleClick on a ListBox does not work
-
13 กันยายน 2550 5:28
Hi,
I have a ListBox in my WPF Application. Now I want to connect the ListBox to a command, which is responsible for opening the current selected item. This command is also use for ToolButtons or the ContextMenu.
I tried the following:
Code Snippet<
Window.CommandBindings><
CommandBindingCommand="ApplicationCommands.Open"
CanExecute="OpenCustomer_CanExecute"
Executed="OpenCustomer_Executed" />
</Window.CommandBindings>
<
ListBox ItemsSource="{Binding ...}" ItemTemplate="{StaticResource ...}"><
ListBox.InputBindings><
KeyBindingKey="Enter"
Command="ApplicationCommands.Open" />
<MouseBinding
MouseAction="LeftDoubleClick"
Command="ApplicationCommands.Open" />
</ListBox.InputBindings>
</ListBox>
If I press ENTER the command will be executed. But the MouseDoubleClick does not work.
ตอบทั้งหมด
-
26 กันยายน 2551 8:51Hi Joe,
Did you find a solution to the ListBox consuming the LeftDoubleClick MouseAction? I'm looking into it now but thought you may have an answer.
cheers.
Dennis -
5 เมษายน 2554 21:56
I have the same probleme.
The listbox consumes the Mouse Double Click only when the Double Click is on a Listbox Item. If die Double Click is inside the free space of the ListBox, where no ListBox Item is, the click is handled appropriate.
-
5 เมษายน 2554 23:48
I think you may use attached property to achieve this. The below thread has a discussion about the same.
http://stackoverflow.com/questions/821564/double-click-a-list-item
Ajosh Jose- เสนอเป็นคำตอบโดย alexfiftyfour 6 เมษายน 2554 6:58
-
6 เมษายน 2554 6:58
Hi Jose!
Nice solution :-)
I have another one:
<ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Name}"> <TextBlock.InputBindings> <MouseBinding Command="somecommand" Gesture="LeftDoubleClick"/> </TextBlock.InputBindings> </TextBlock> </DataTemplate> </ListBox.ItemTemplate>
Is more inefficient than your solution, but works good too.
- เสนอเป็นคำตอบโดย jdeep84 2 สิงหาคม 2555 12:49
-
12 เมษายน 2554 6:08
Hi all,
I was about to create a new thread for the mouse double click issue, but I stumbled upon this thread. I also have the same problem with mouse double click on a listbox. The keyboard "enter" works fine though. I tried the solution suggested by Alex, but it doesn't seem to work. I can't use attached property since I need to be able to call my non-static methods in order to do some operation. So, is there any other alternative for this issue or am I missing something?
Regards,
Vikhyath
-
12 เมษายน 2554 7:56
Hi Vikhyath,
I used my obove solution, but I think it is inefficient. So now I do it the old way again, by registering to the MouseDoubleClick event in code. I tryed a lot, but I didn't get it to work with commands in an efficient way. So I call the commands in code.
_ListBox.MouseDoubleClick += new MouseButtonEventHandler(_ListBox_MouseDoubleClick);void _ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e) { if (e.ChangedButton == MouseButton.Left) { if (MyCommands.SomeCommand.CanExecute(null, _ListBox)) { e.Handled = true; MyCommands.SomeCommand.Execute(null, _ListBox); } } }
And don't forget to set the commandbinding somewhere, maybe at your listbox or at some parent control of the listbox. You can do this in XAML too.
this.CommandBindings.Add(new CommandBinding(MyCommands.SomeCommand, new ExecutedRoutedEventHandler(SomeCommand_Executed), new CanExecuteRoutedEventHandler(SomeCommand_CanExecute)));
-
12 เมษายน 2554 8:41
Thanks Alex!
But, I am using MVVM pattern, so I guess your solution may not be feasible. Any idea on how this can be achieved in MVVM?
I have been struggling with this issue for quite sometime now...
Vikhyath -
12 เมษายน 2554 9:17
Hey Vikhyath,
I'm very sorry, but I don't know how to solve this in MVVM.
This bug is realy annoying.
-
12 เมษายน 2554 14:28
No problem Alex.
Is there anyone here who has a solution to this problem?
Vikhyath -
12 เมษายน 2554 17:57
Have a look at EventBurglar (at the bottom of the thread) you can use it to hook up events to a command in your view model.
If you put it on the listbox it will use the listbox datacontext to resolve the handler. If you put it on the ListBoxItem it will use the ListBoxItem's datacontext to resolve the handler.
-
13 เมษายน 2554 5:17
See this documentation.
It's written for the Catel MVVM framework, but it will work for all frameworks that support EventToCommand.
Geert van Horrik - CatenaLogic
Visit my blog: http://blog.catenalogic.com
Looking for a free open-source MVVM framework for WPF, Silverlight and Windows Phone 7? Check out Catel! -
24 สิงหาคม 2554 10:01That's it! Thank you for the link!
-
8 พฤษภาคม 2555 12:09
I used a small library 'AttachedCommandBehavior' for this.
xmlns:acb="clr-namespace:AttachedCommandBehavior;assembly=AttachedCommandBehavior" <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="acb:CommandBehavior.Event" Value="MouseDoubleClick" /> <Setter Property="acb:CommandBehavior.Command" Value="{Binding DataContext.DoubleClickCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" /> <Setter Property="acb:CommandBehavior.CommandParameter" Value="{Binding}" /> </Style>You can get this library from http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/
If you don't want to use this library, you can write a attached behavior on your own.
Please mark it as answer it if suffices your requirement. -
13 กรกฎาคม 2555 3:35I have the same problem, the hope can help me!!!! Thank you!
-
2 สิงหาคม 2555 12:49Alex's solutions works perfect for me...Thanks!
-
5 พฤศจิกายน 2555 15:02
Hi
I used Expression SDk 4.0
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick" SourceName="CaravanasListBox">
<i:InvokeCommandAction Command="{Binding AccionesToolbarCommand}" CommandParameter="{x:Static local:OpcionesBarra.MostrarDetalle}" />
</i:EventTrigger>
</i:Interaction.Triggers>Jaimir G.