© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
1 reply
SilverBrother

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
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;

}
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.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ WPF Toolbox in visual does not autofill
C#CC# / help
3y ago
WPF Icon
C#CC# / help
10mo ago
❔ WPF add Icon
C#CC# / help
3y ago
❔ ✅ Custom Fonts Not Working [ Visual Studio - WPF ]
C#CC# / help
3y ago