Question

I've been successful at placing an ellipse on a canvas, but it shows up at the top left corner of it despite my trying different methods of moving it. In this attempt, I am trying to move it to the center of the canvas, but I would like to be able to move it anywhere in the canvas.

private Ellipse drawEllipse(Canvas aCanvas)
{
   Ellipse newEllipse= new Ellipse();
   newEllipse.Width = 40;
   newEllipse.Height = 40;
   newEllipse.Fill = new SolidColorBrush(Colors.Aquamarine);
   aCanvas.Children.Add(lEllipse);
   newEllipse.SetValue(Canvas.LeftProperty, aCanvas.ActualWidth / 2.0);
   newEllipse.SetValue(Canvas.TopProperty, aCanvas.ActualHeight / 2.0);

   return newEllipse;
}
Was it helpful?

Solution

Try this:

Canvas.SetLeft(newEllipse, aCanvas.ActualWidth/2.0);
Canvas.SetTop(newEllipse, aCanvas.ActualHeight/2.0);

I didn't try it, but it worked for me all the time.

Edit: Ahh and you should probably first add the ellipse to the canvas before moving the ellipse around.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top