Jonathan Allen
Jonathan Allen

Reputation: 70337

WPF: Is there a way to directly get the Window object a control is on?

Is there a way to directly get the Window object a control is on?

Upvotes: 1

Views: 549

Answers (3)

batzen
batzen

Reputation: 190

A bit late, but the following works pretty fine for me.

var ownerWindow = Window.GetWindow(yourControl);

Upvotes: 0

SwissCoder
SwissCoder

Reputation: 2590

Most likely you are on the applications active window in the moment you interact with the control.

So in that case, this other post on stackoverflow could be helpful too:

stackoverflow: get active window(s) in wpf

Upvotes: 1

kiwipom
kiwipom

Reputation: 7709

Assuming the Control is directly below the Window, you can call GetParent on the LogicalTreeHelper.

You may, of course, need to Use the LogicalTreeHelper to traverse the tree if the Control is not a direct descendant.

Upvotes: 5

Related Questions