How can I find the center of a UIElement
in wpf?
Asked
Active
Viewed 6,586 times
2 Answers
6
You can get absolute position of the control like this
Point relativePoint = myVisual.TransformToAncestor(rootVisual)
.Transform(new Point(0, 0));
where myVisual
is your control and rootVisual
is the parent control(see Get Absolute Position of element within the window in wpf), so you can find the center of the uielement like this
Point pt = new Point(relativePoint.X + myVisual.ActualWidth/2, relativePoint.Y + myVisual.ActualHeight/2);

Community
- 1
- 1

Arsen Mkrtchyan
- 49,896
- 32
- 148
- 184
-
3Does not work for a Path. Still looking for an answer. Thanks – np. Dec 03 '09 at 12:27
0
Building off @ArsenMkrt,
Point center = myVisual.TransformToAncestor(rootVisual).Transform(new Point(myVisual.ActualWidth / 2, myVisual.ActualHeight / 2));

Paymahn Moghadasian
- 9,301
- 13
- 56
- 94