views:

1942

answers:

2

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?

+3  A: 

Try Marlon Grech's attached command behaviors.

HTH, Kent

Kent Boogaart
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
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
A: 

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