Getting A UserControl Parent Window In WPF

This is a very simple blog entry, but I want to keep a record of it somewhere. So here is the story, as part of an ongoing article that i’m working on I have a UserControl that is on main Window (Window1 say), and from a Button on the main Window (Window1 say)  hosted UserConrtrol, I want to be able to show a Owned Window (SomeWindow), which is owned by the main Window (Window1 say). I must have been having a bad hair day (actually my hair is cool today, some new Gel arrived), but this took me 5 minutes longer than it should have. So I thought I’d write it down.

It’s ever so simple. Oh do I feel stupid

In the UserControl, event where the new Owned window will be shown

   1:  SomeWindow win= new SomeWindow();
   2:  win.Owner = GetTopParent();
   3:  win.ShowInTaskbar = false;
   4:  win.WindowStartupLocation =
   5:       WindowStartupLocation.CenterOwner;
   6:  win.ShowDialog();

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

 
And then the simple method that fetches the UserControls (who wants to
show the Window (SomeWindow)) parent Window, that can be used as the
Owner property for the new Window (SomeWindow)
 

   1:          private Window GetTopParent()
   2:          {
   3:              DependencyObject dpParent = this.Parent;
   4:              do
   5:              {
   6:                  dpParent = LogicalTreeHelper.GetParent(dpParent);
   7:              } while (dpParent.GetType().BaseType != typeof(Window));
   8:              return dpParent as Window;
   9:          }

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

And that’s it. I now have a main Window (Window1 say) that hosts a UserControl, from which I launch a 2nd Window (SomeWindow) that is owned by the main Window (Window1 say)

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

Advertisements

13 thoughts on “Getting A UserControl Parent Window In WPF

  1. Robert says:

    It could be even simpler ’cause the Window class contains the static method GetWindow() that does the same for any dependency object located in a visual tree of some window.

  2. sacha says:

    Cool..I didnt know about that. Thats really handy.

    Thanks for pointing that out

  3. Jeff says:

    Simple, but so helpful! Thanks for posting.

  4. sacha says:

    Jeff as someone pointed out, you should really be using Wimdow.GetWindow() static method. But this technique would work for other types.

  5. Ear hair removal…

    Very nice article and thoughts. Looks like you know what’s going on. Will stop again….

  6. hardeep says:

    Hi

    I am working on wpf & Mvvm based project. I have to set owner property of child window but I am not able to get the parentwindow reference.
    Do you have any idea.
    the above solution uses “this.parent” but i didn’t get this “parent” option because I am using MVVM pattern.

    Any Idea??

    Thx in adv.

    • sacha says:

      What MVVM framework are you using, any? I can not answer unless I know more about what you are doing

  7. Ibus says:

    thanks dude

    though can i suggest that this might be better.

    DependencyObject parent = this.Parent;

    while (parent.GetType().BaseType != typeof(Window))
    {
    parent = LogicalTreeHelper.GetParent(parent);
    }

    return parent as Window;

    above you are getting parent twice before doing a test

    thanks for the snippet though

    cheers

  8. woax says:

    thx good tipp with

    var window = Window.GetWindow(this);

  9. bob says:

    Could you use this.FindVisualParent() instead?

  10. You could also create an extension to find the parent window of any control. This is handy for MVVM style XAML apps.

    myXAMLControl.Parent.FindParent
    or
    myXAMLControl.Parent.FindParent

    C# Extension method.

    [System.Runtime.CompilerServices.Extension()]
    public T FindParent(System.Windows.DependencyObject dep) where T : System.Windows.DependencyObject
    {

    while ((dep != null) && !(dep is T)) {
    dep = System.Windows.Media.VisualTreeHelper.GetParent(dep);
    }

    return dep as T;

    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: