Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify the position of an Ellipse shape on a canvas in WPF?

I am programmatically creating an Ellipse shape but I can't find any property that specifies its position. Lines have X1, Y1, X2, Y2, but there is no Center, Position, X, Y, etc on an Ellipse shape. How can I do this?

like image 917
Joan Venge Avatar asked Apr 08 '11 00:04

Joan Venge


People also ask

How do you draw an ellipse in WPF?

To draw an ellipse, create an Ellipse element and specify its Width and Height. Use its Fill property to specify the Brush that is used to paint the interior of the ellipse. Use its Stroke property to specify the Brush that is used to paint the outline of the ellipse.

Which library provides the classes used to implement basic XAML shapes for example ellipse path and rectangle?

Windows Presentation Foundation (WPF) provides support for drawing basic shapes using the Shape class. Shape objects derive from FrameworkElement and as such, participate in the layout system and can be drawn simply using Extensible Application Markup Language (XAML).

How do you draw a circle in XAML?

So how can we make one in XAML without acutally cropping the image? It's simple. First, use "ellipse" to draw a circle, make it's width equal to height, that makes a circle. Ellipse has many options on "Fill", such as solid color, gradient color, image, and web.


2 Answers

Putting shapes in arbitrary places on the screen should probably be done so in a Canvas Panel (see @phoog's response). But if you're placing this in a Grid or some other panel instead, you could always modify the Margin property to place it where you want it.

If you wanted to do so by specifying the center point instead of the top left corner of the ellipse, you could do this:

Ellipse CreateEllipse(double width, double height, double desiredCenterX, double desiredCenterY)
{
    Ellipse ellipse = new Ellipse { Width = width, Height = height };
    double left = desiredCenterX - (width / 2);
    double top  = desiredCenterY - (height/ 2);

    ellipse.Margin = new Thickness(left, top, 0, 0);
    return ellipse;
}

I haven't checked that this does exactly what you want in the compiler, but hopefully you get the idea. Again, using Canvas would be the prefered method over using Margin inside of a non-Canvas panel, but the same principle of calculating left and top would still apply:

Canvas.SetLeft(ellipse, desiredCenterX - (width/2))
Canvas.SetTop(ellipse, desiredCenterY - (height/2))
like image 190
viggity Avatar answered Oct 05 '22 02:10

viggity


Canvas.Left and Canvas.Top. It's all in the documentation on "How to Draw an Ellipse or a Circle" http://msdn.microsoft.com/en-us/library/ms751563.aspx

In C# code, the syntax would be this:

void CreateCanvasWithEllipse(double desiredLeft, double desiredTop)
{
    Canvas canvas = new Canvas();
    Ellipse ellipse = SomeEllipseConstructionMethod();
    Canvas.SetLeft(ellipse, desiredLeft);
    Canvas.SetTop(ellipse, desiredTop);
}
like image 21
phoog Avatar answered Oct 05 '22 02:10

phoog