How to bind a command in WPF to a double click event handler of a control?
Solution 1
Try Marlon Grech's attached command behaviors.
Solution 2
<Button>
<Button.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="YourCommand" />
</Button.InputBindings>
</Button>
http://thejoyofcode.com/Invoking_a_Command_on_a_Double_Click_or_other_Mouse_Gesture.aspx
Solution 3
it's simple let's use the MVVM way: I'm using here MVVM Light which is easy to learn and strong.
1.put the following lines the xmlns declarations :
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;
assembly=GalaSoft.MvvmLight.Extras.WPF4"
2.define your textblock just like this:
<textBlock text="Text with event">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<GalaSoft_MvvmLight_Command:EventToCommand
Command="{Binding Edit_Command}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</textBlock>
3.then write your command code in your viewmodel !!!
ViewModel1.cs
Public RelayCommand Edit_Command
{
get;
private set;
}
Public ViewModel1()
{
Edit_Command=new RelayCommand(()=>execute_me());
}
public void execute_me()
{
//write your code here
}
I hope that works for you as I have used it in Real ERP application
Solution 4
I also had a similar issue where I needed to bind the MouseDoubleClick event of a listview to a command in my ViewModel.
The simplest solution I came up is putting a dummy button which has the desired command binding and calling the Execute method of the button's command in the eventhandler of the MouseDoubleClick event.
.xaml
<Button Visibility="Collapsed" Name="doubleClickButton" Command="{Binding Path=CommandShowCompanyCards}"></Button>
<ListView MouseDoubleClick="ListView_MouseDoubleClick" SelectedItem="{Binding Path=SelectedCompany, UpdateSourceTrigger=PropertyChanged}" BorderThickness="0" Margin="0,10,0,0" ItemsSource="{Binding Path=CompanyList, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" HorizontalContentAlignment="Stretch" >
codebehind
private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
doubleClickButton.Command.Execute(null);
}
It is not straightforward but it is really simple and it works.
bluebit
Updated on July 05, 2022Comments
-
bluebit almost 2 years
I need to bind the double click event of a textblock (or potentially an image as well - either way, its a user control), to a command in my ViewModel.
TextBlock.InputBindings does not seem to bind correctly to my commands, any help?
-
bluebit over 14 yearsI already have three helper classes in my project. I just wish WPF had full support for all of these things that developers want to do with it, but I guess that will come in time hey. Thanks, that worked :)
-
aroon65 over 14 yearsI agree on your general sentiment - it's a little frustrating that support for MVVM isn't more baked into WPF. Most seasoned WPF developers have built up their own little library of auxiliary helper stuff. The gap in functionality is even greater if you're doing Silverlight!
-
Sheridan over 12 years+1 Personally, I think this answer should be the one marked as correct. Not only is it by far the simplest, but it can also be used with MVVM by binding to a
ICommand
in a view model:<MouseBinding Gesture="LeftDoubleClick" Command="{Binding YourCommand}" />
-
D_Guidi over 12 yearsnot working (maybe on some objects only) until you made this: telerik.com/community/forums/wpf/gridview/…
-
Claudiu Constantin about 12 yearsNice alternative to the messy attached behaviors :)
-
PaulMolloy over 11 yearsThat's just a whole bag of awesome! It even applies to other controls out of the box. e.g. <TextBlock.InputBindings>
-
Simon K. over 8 yearsA bit messy with these 'gala-soft-stuff' but the Interaction-approach is good!
-
user99999991 over 8 yearsYou can't, however, seem to use this in a Style.
-
Dabblernl about 5 yearsWow, 10 years into WPF programming I have always solved this, and seen this solved, by reverting to the mouse down events.
-
Anthony Nichols over 4 yearsDon't link to external site alone - please include the answer. If the site ever goes away this becomes useless.
-
Alexandru Dicu about 2 yearsYou can use it in a style but you have to use RelativeSource and find the actual DataContext. A style does not have a DataContext.