np.
np.

Reputation: 2379

Find the center of a uielement in wpf

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

Upvotes: 8

Views: 6847

Answers (2)

Paymahn Moghadasian
Paymahn Moghadasian

Reputation: 10349

Building off @ArsenMkrt,

Point center = myVisual.TransformToAncestor(rootVisual).Transform(new Point(myVisual.ActualWidth / 2, myVisual.ActualHeight / 2));

Upvotes: 0

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50752

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

Upvotes: 6

Related Questions