1

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

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447

3 Answers3

5

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.

kiwipom
  • 7,639
  • 37
  • 37
1

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

Community
  • 1
  • 1
SwissCoder
  • 2,514
  • 4
  • 28
  • 40
  • That looks like it would be very fragile for my use case, but I;ll give you a point because it would be really useful when displaying a pop-up window with CenterOwner set. – Jonathan Allen Jul 26 '10 at 22:41
0

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

var ownerWindow = Window.GetWindow(yourControl);
batzen
  • 161
  • 2
  • 4