My image is 20x20 pixels and when it's loaded it stretchs to fill the space of the grid which is WxH. When I click on the image I collect the mouse locations. Later I use them to draw the dots on the 20x20 image and save it to the file. But, because the image was stretched, I need to map the mouse location on WxH image to my original 20x20 image.
What is the proper way to do this in MVVM? My thoughts are:
- Bind to the
ActualWidth
andActualHeight
of the image and then calculate the ratio between the original image and the stretched image. - Bind to the
ScaleX
andScaleY
ofScaleTransform
of the image. But, it always returns 1 forScaleX
andScaleY
.
XAML
<Grid>
<Image Source="{Binding MyImage}" Stretch="Fill">
<Image.RenderTransform>
<ScaleTransform ScaleX="{Binding ScaleX, Mode=OneWayToSource}" ScaleY="{Binding ScaleY, Mode=OneWayToSource}" />
</Image.RenderTransform>
</Image>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<cmd:EventToCommand Command="{Binding GetMouseLocationCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Grid>
CODE
public class VM
{
public double ScaleX { get; set; }
public double ScaleY { get; set; }
}