WPF mousePos and icon.Margin align. Visual rep does not.

So I'm trying to make an icon visible in a map at the same position as the mouseclick, but it always appears somewhere in the bottom right quadrant. What am I not seeing? It is like there is a layer askewed to the map .Here's the xaml bit
<Viewbox x:Name="ViewMap" MouseLeftButtonDown="Viewbox_MouseLeftButtonDown" Stretch="Uniform">
<!-- Your image display content -->
<Image x:Name="imageControl" MouseWheel="ImageControl_MouseWheel" Loaded="ImageControl_Loaded" Stretch="Uniform">
</Image>
</Viewbox>
<Viewbox x:Name="ViewMap" MouseLeftButtonDown="Viewbox_MouseLeftButtonDown" Stretch="Uniform">
<!-- Your image display content -->
<Image x:Name="imageControl" MouseWheel="ImageControl_MouseWheel" Loaded="ImageControl_Loaded" Stretch="Uniform">
</Image>
</Viewbox>
There's only a grid and a page as ancestors. No attributes on the grid. And here are the C# code
C#
private void Viewbox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Get the cursor position relative to the mainGrid
Point cursorPosition = e.GetPosition(imageControl);
Debug.WriteLine($"{cursorPosition}");
// Show the icon at the cursor position
ShowIcon(calibrationIcon, cursorPosition);
}
private void ShowIcon(Image icon, Point position)
{
icon.Margin = new Thickness(position.X - icon.ActualWidth / 2, position.Y - icon.ActualHeight / 2, 0, 0);
Debug.WriteLine($"{icon.Margin}");
// Show the icon
icon.Visibility = Visibility.Visible;

}
C#
private void Viewbox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Get the cursor position relative to the mainGrid
Point cursorPosition = e.GetPosition(imageControl);
Debug.WriteLine($"{cursorPosition}");
// Show the icon at the cursor position
ShowIcon(calibrationIcon, cursorPosition);
}
private void ShowIcon(Image icon, Point position)
{
icon.Margin = new Thickness(position.X - icon.ActualWidth / 2, position.Y - icon.ActualHeight / 2, 0, 0);
Debug.WriteLine($"{icon.Margin}");
// Show the icon
icon.Visibility = Visibility.Visible;

}
Let me know if you need more for this to make sense. Thank you so much in advance.
1 Reply
Buddy
Buddy4mo ago
Margin is the spacing between elements it is not the absolute position