alansiqueira27
alansiqueira27

Reputation: 8506

Is it possible to identify which window/usercontrol owns a FrameworkElement?

Is it possible to identify which window/usercontrol owns a FrameworkElement?

Thank you.

Upvotes: 2

Views: 2279

Answers (3)

Sam
Sam

Reputation: 1692

Or you could just use the static helper method GetWindow(DependencyObject dependencyObject) from the Window class. This method returns the Window object that hosts the element.

Upvotes: 8

K Mehta
K Mehta

Reputation: 10543

While Fischermaen's answer works for most cases, it'd break if you have templates in your code. In that case, you'd have to check both, .Parent and .TemplatedParent when you traverse upward

Upvotes: 3

Fischermaen
Fischermaen

Reputation: 12458

You can "walk up" using the .Parent-Property until you find the window or usercontrol. Just check the type of the parent while walking up.

Upvotes: 6

Related Questions