0

Here is my XAML code:

<Grid>
    <Grid.InputBindings>
        <MouseBinding Gesture="LeftDoubleClick" Command="{Binding MouseDblClick}" />
    </Grid.InputBindings>
</Grid>

and my code-behind:

private RelayCommand _MouseDoubleClick;

public ICommand MouseDblClick
{
    get
    {
        if (_MouseDoubleClick == null)
        {
            _MouseDoubleClick = new RelayCommand(param => Clicked());
        }

        return _MouseDoubleClick;
    }
}

private void Clicked()
{
    MessageBox.Show("Works");
}

I want the messageBox to show after I double click the grid. But nothing happens. Where's the mistake?

krlzlx
  • 5,752
  • 14
  • 47
  • 55
Michal_Drwal
  • 526
  • 1
  • 9
  • 26

2 Answers2

3

As icebat mentioned in his comments. Try to set BG of Grid with some Brush

<Grid Background="Green">
    <Grid.InputBindings>
        <MouseBinding Gesture="LeftDoubleClick" Command="{Binding MouseDblClick}" />            
    </Grid.InputBindings>
</Grid>

By default, Grid will have Background="{x:Null}" which is not clickable as mentioned here

Also, Make sure that you have passed the VM object to DataContext of the View

Something like this this.DataContext = new ViewModel();

Community
  • 1
  • 1
Gopichandar
  • 2,742
  • 2
  • 24
  • 54
0

SUGGESTION OF ANOTHER SOLUTION:

I'm not a fan of Commands, I have another solution based on Trigger Events if it can help you:

1) Add a reference to the System.Windows.Interactivity and Microsoft.Expression.Interactions DLLs (found it in C:\Program Files (x86)\Microsoft SDKs\Expression\Blend.NETFramework\v4.0\Libraries)

2) Add this to your XAML file:

 xmlns:l="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
 xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"

3) Add this inside your Grid:

<Grid...>
     <l:Interaction.Triggers>
          <l:EventTrigger EventName="DoubleClick">
                 <ei:CallMethodAction TargetObject="{Binding}"
                          MethodName="Clicked" />
          </l:EventTrigger>
     </l:Interaction.Triggers>
...
</Grid>

4) Make your Method "Clicked()" Public

I hope this will help you.

NTinkicht
  • 972
  • 2
  • 12
  • 34