7

How can I find the center of a UIElement in wpf?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
np.
  • 2,319
  • 4
  • 31
  • 36

2 Answers2

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
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