C
C#10mo ago
SilverBrother

WPF Mouse Position and Visible dot not the same place

I'm trying to make a simple desktop application in WPF and I'm having problems placing a dot the same place my mouse is. First picture is when i click almost top left. Second picture is almost clicking bottom right. Third picture is my Xaml file. Here is the relevant C# code
private void MainGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
// Get the mouse click position relative to the mainGrid
Point clickPosition = e.GetPosition(mainGrid);

// Show a dot at the click position
ShowDot(dotEnd, clickPosition);
}
private void ShowDot(Ellipse dot, Point position)
{
dot.Margin = new Thickness(position.X - dot.Width / 2, position.Y - dot.Height / 2, 0, 0);

// Show the dot
dot.Visibility = Visibility.Visible;
}
private void MainGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
// Get the mouse click position relative to the mainGrid
Point clickPosition = e.GetPosition(mainGrid);

// Show a dot at the click position
ShowDot(dotEnd, clickPosition);
}
private void ShowDot(Ellipse dot, Point position)
{
dot.Margin = new Thickness(position.X - dot.Width / 2, position.Y - dot.Height / 2, 0, 0);

// Show the dot
dot.Visibility = Visibility.Visible;
}
No description
No description
No description
1 Reply
Buddy
Buddy10mo ago
Margin is not the absolute position It is the spacing between elements If you want absolute position set Left, Right, Top, and Bottom properties
Want results from more Discord servers?
Add your server