67

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
  • 2,909
  • 7
  • 31
  • 41

4 Answers4

274
<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

Legz
  • 2,769
  • 1
  • 11
  • 2
  • 45
    +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: `` – Sheridan Nov 14 '11 at 22:06
  • not working (maybe on some objects only) until you made this: http://www.telerik.com/community/forums/wpf/gridview/bind-double-click-to-a-viewmodel-command.aspx – D_Guidi Dec 06 '11 at 08:33
  • 4
    Nice alternative to the messy attached behaviors :) – Claudiu Constantin Mar 13 '12 at 19:32
  • 7
    That's just a whole bag of awesome! It even applies to other controls out of the box. e.g. – PaulMolloy Oct 02 '12 at 14:09
  • 2
    You can't, however, seem to use this in a Style. – user99999991 Dec 15 '15 at 18:35
  • Wow, 10 years into WPF programming I have always solved this, and seen this solved, by reverting to the mouse down events. – Dabblernl Feb 03 '19 at 10:20
9

Try Marlon Grech's attached command behaviors.

Tim Cooper
  • 144,163
  • 35
  • 302
  • 261
Kent Boogaart
  • 165,446
  • 34
  • 376
  • 376
  • 3
    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 :) – bluebit Aug 18 '09 at 13:32
  • 2
    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! – Kent Boogaart Aug 18 '09 at 13:36
  • 2
    Don't link to external site alone - please include the answer. If the site ever goes away this becomes useless. – Anthony Nichols Dec 03 '19 at 16:25
7

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

Indy9000
  • 8,188
  • 2
  • 26
  • 34
Adam
  • 2,973
  • 25
  • 22
2

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.

irem
  • 21
  • 1