How to bind a command in WPF to a double click event handler of a control?

75,727

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.

Share:
75,727
bluebit
Author by

bluebit

Updated on July 05, 2022

Comments

  • bluebit
    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
    bluebit over 14 years
    I 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
    aroon65 over 14 years
    I 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
    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
    D_Guidi over 12 years
    not working (maybe on some objects only) until you made this: telerik.com/community/forums/wpf/gridview/…
  • Claudiu Constantin
    Claudiu Constantin about 12 years
    Nice alternative to the messy attached behaviors :)
  • PaulMolloy
    PaulMolloy over 11 years
    That's just a whole bag of awesome! It even applies to other controls out of the box. e.g. <TextBlock.InputBindings>
  • Simon K.
    Simon K. over 8 years
    A bit messy with these 'gala-soft-stuff' but the Interaction-approach is good!
  • user99999991
    user99999991 over 8 years
    You can't, however, seem to use this in a Style.
  • Dabblernl
    Dabblernl about 5 years
    Wow, 10 years into WPF programming I have always solved this, and seen this solved, by reverting to the mouse down events.
  • Anthony Nichols
    Anthony Nichols over 4 years
    Don't link to external site alone - please include the answer. If the site ever goes away this becomes useless.
  • Alexandru Dicu
    Alexandru Dicu about 2 years
    You can use it in a style but you have to use RelativeSource and find the actual DataContext. A style does not have a DataContext.