Get click position over command binding

KDiTraglia had the right pointer for me... In any case I had some issues with defining the actions and binding to my ViewModel. I'll post my solution in case someone else has some problems. Here's what I've done in the xaml:

<Grid Width="100" Height="100" Grid.Column="2" Grid.Row="2" x:Name="TargetGrid">
    <Grid>
        <Grid.InputBindings>
            <MouseBinding Gesture="LeftClick" Command="{Binding Path=TargetClick}" CommandParameter="{Binding ElementName=TargetGrid}" />
        </Grid.InputBindings>
    </Grid>
</Grid>

I create the UserControl and bind it to the ViewModel. In the ViewModel I implement and create the following command:

public class PositioningCommand : ICommand
{
    public PositioningCommand()
    {
    }

    public void Execute(object parameter)
    {
        Point mousePos = Mouse.GetPosition((IInputElement)parameter);
        Console.WriteLine("Position: " + mousePos.ToString());
    }

    public bool CanExecute(object parameter) { return true; }

    public event EventHandler CanExecuteChanged;
}

public PositioningCommand TargetClick
{
    get;
    internal set;
}

How about this?

private void MinimapClick(object parameter)
{
    Point mousePos = Mouse.GetPosition(myWindow);
}

If you don't have a reference to the window you could send it as a parameter (or use whatever reference point you want).

Tags:

C#

Wpf

Command

Mvvm