0

I have a button that's display an image :

<Button Command="{Binding Model.ZoomOnImage}">
    <Image Source="{Binding Model.ImageSource}" Stretch="Uniform" />
</Button>

In my Model class, I have this command :

private ICommand _zoomOnImage;
public ICommand ZoomOnImage
{
    get
    {
        if (_zoomOnImage == null)
            _zoomOnImage = new RelayCommand(Zoom, CanZoom);
        return _zoomOnImage;
    }
}
private void Zoom() {...}
private bool CanZoom() {...}

When user click on the button, I want to get the click [X; Y] coordinates (to perform Zoom(int X, int Y)). How can I modify my code to achieve this ?

A.Pissicat
  • 3,023
  • 4
  • 38
  • 93
  • you could use eventhandler for click event and then invoke your command with mouse positions (X and Y). That will keep it MvvM :-) – XAMlMAX Sep 10 '18 at 10:33
  • @Diado I looked your link but i'm not sure it respects MvvM. There is no way to provide X,Y in the view ? – A.Pissicat Sep 10 '18 at 12:18
  • 1
    If you look at [this answer](https://stackoverflow.com/a/10332880/2029607) which is from the Diado's link, there you can get the mouse coordinates, you would ideally create a custom class that will hold those values so when you pass that into your VM it doesn't rely on `PresentationFramework.dll`. HTH – XAMlMAX Sep 10 '18 at 12:24

1 Answers1

1

use custom button or create behaviour if you want. also add property into your viewmodel public Point MousePosition{get;set;}

<local:CustomButton  Command="{Binding Model.ZoomOnImage}" MousePosition="{Binding MousePosition}">
    <Image Source="{Binding Model.ImageSource}" Stretch="Uniform" />
</local:CustomButton>

public class CustomButton : Button
{

    public Point MousePosition
    {
        get { return (Point)GetValue(MousePositionProperty); }
        set { SetValue(MousePositionProperty, value); }
    }
    // Using a DependencyProperty as the backing store for MousePosition.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MousePositionProperty = DependencyProperty.Register("MousePosition", typeof(Point), typeof(CustomButton), new FrameworkPropertyMetadata(new Point(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    protected override void OnClick()
    {
        base.OnClick();
        MousePosition = Mouse.GetPosition(this);
    }
}
A.Pissicat
  • 3,023
  • 4
  • 38
  • 93
Ilya Grigoryan
  • 229
  • 1
  • 5