Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Can you show me how to draw circle centered at a location on a Canvas in WPF?

Here is an example of how to draw an ellipse. But, it doesn't have ability to specify center.

C#
// Create a red Ellipse.
Ellipse myEllipse = new Ellipse();
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Colors.Black;
myEllipse.Fill = mySolidColorBrush;
myEllipse.StrokeThickness = 2;
myEllipse.Stroke = Brushes.Black;

// Set the width and height of the Ellipse.
myEllipse.Width = 100;
myEllipse.Height = 100;

// How to set center of ellipse???

canvas1.Children.Add(myEllipse);


Thanks
Posted
Updated 30-Jul-13 15:44pm
v2
Comments
Sergey Alexandrovich Kryukov 30-Jul-13 17:03pm    
And what's wrong with your code?
—SA
Maciej Los 30-Jul-13 17:08pm    
Good question!
My virtual 5!
Sergey Alexandrovich Kryukov 30-Jul-13 17:11pm    
:-)
Jaxam 30-Jul-13 21:42pm    
The code I have above doesn't specify a location or center to draw the ellipse.

The problem is not Ellipse but Canvas. WPF positioning is different from other libraries; a shape, like any other UIElement, does not specify location. The location is defined by layout. In case of Canvas, location of the children is defined by its attached properties. Please see the methods you can use to change the child object (which in this case play the role of dependency object) location:
http://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.setleft.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.settop.aspx[^].

See also:
http://msdn.microsoft.com/en-us/library/bb613548.aspx[^],
http://msdn.microsoft.com/en-us/library/ms745058.aspx#LayoutSystem_Overview[^].

To work with WPF, you often need to understand such an advanced thing as dependency property and its specialized form called attached property:
http://msdn.microsoft.com/en-us/library/ms752914.aspx[^],
http://msdn.microsoft.com/en-us/library/ms749011.aspx[^].

—SA
 
Share this answer
 
v2
Comments
Maciej Los 31-Jul-13 1:36am    
Very, very good answer - as always!
+5
Sergey Alexandrovich Kryukov 31-Jul-13 9:41am    
Thank you, Maciej.
—SA
I would suggest you to read this article: Shapes and Basic Drawing in WPF Overview[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-Jul-13 17:04pm    
Useful suggestion, I think, a 5.
—SA
Maciej Los 30-Jul-13 17:06pm    
Thank you, Sergey ;)
Sergey Alexandrovich Kryukov 30-Jul-13 22:45pm    
After some more of looking at the question, I realized that OP's confusion is related to things pretty much fundamental to WPF: attached properties (and hence dependency properties), which makes the question more serious. UIElement-based classes really lack their own locations.

Practical answer is very simple, but the explanation of why it works like that is not. This is not so trivial stuff. Please see my answer.
—SA
I would think that you have to do the UpdateLayout() to get the window to refesh.
 
Share this answer
 
Comments
Clifford Nelson 30-Jul-13 17:13pm    
Somebody downvoted. It would be a nice idea to know why. Do you know for sure this will not work, or do you have the real solution? If so, please share it.
Manfred Rudolf Bihy 31-Jul-13 13:01pm    
All I can tell you it's a high rep downvote, I checked by trying an upvote.
I'm not sure though if your post is actually useful. Seems SA put OP on the right track.

Cheers!
Clifford Nelson 31-Jul-13 13:05pm    
Yes it seems you are right. I did not try to test my suggestion, and it was only a suggestion. Now that SA has put the input, I can see the issue. I would have probably used a canvas in the first place, so would have avoided having the problem. A low vote with no information is useless.
Thank you guys for helping. I got it working. You guy are right about the object property in canvas that I have to play with. In which I looked into the Margin and found the solution I was looking for.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900