I am trying to draw a rectangle at the point in a canvas where the user clicks. I want my window to be scale- able so I have placed the Canvas in a viewbox but this doesn't draw the rectangle on the specified point but is variable based on the scaling of the viewbox. here is my code
<Viewbox MouseLeftButtonDown="Viewbox_MouseLeftButtonDown" Width="300" Height="300">
<Canvas Name="Surface" MouseLeftButtonDown="Surface_MouseRightButtonDown" Background="Transparent" Width="300" Height="300"></Canvas>
</Viewbox>
here is the viewbox mouse down event
private void Viewbox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Rectangle rect = new Rectangle { Width = 10, Height = 10, Fill = Brushes.Black };
Surface.Children.Add(rect);
Canvas.SetLeft(rect, e.GetPosition(this).X);
Canvas.SetTop(rect, e.GetPosition(this).Y);
}
The result is the same even if I call the Canvas mouse event.