• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2035
  • Last Modified:

WPF C# Find Window Instance

I have a click event button handler in which I need to find out the instance (not the class) of the window in which the button resides.

I know that Application.Current.MainWindow will tell me the name of the main window but the application launches several windows so there is no guarantee this button click occurred in the main window.

(P.S. the button resides on a page which is held in a frame of the window)

0
esc_toe_account
Asked:
esc_toe_account
  • 4
1 Solution
 
Gururaj BadamCommented:
((Button)sender).Owner should give you the instance of the owner?

Other ways try LogicalTreeHelper.GetParent(sender as DependencyObject);
0
 
esc_toe_accountAuthor Commented:
Thanks for responding.

((Button)sender.Owner is not valid as Button does not have an Owner property.

And LogicalTreeHelper.GetParent only goes up the tree as far as the page in which the button resides. (Curious as to why it doesn't go up at least to the frame in which in which the page is held?)
0
 
esc_toe_accountAuthor Commented:
Don't know if this is relevant but the page is navigated to using the navigation service:

theWindow.frame.Navigate(myPage);

where the button in question resides on myPage;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SriVaddadiCommented:
RoutedEventArgs has a property Source which could be type casted to button and use the Parent property of the button

Buttong b = e.Source as Button;
b.Parent as Window;
0
 
esc_toe_accountAuthor Commented:
Not sure what I was looking at before. Of-course, VisualTreeHelper does go all the way up the tree. So your suggestion was accruate. In fact, here is a static method I wrote to browse up the tree looking for a specific type of control object (e.g. Window, Frame, etc.)

public static DependencyObject GetVisualParent(DependencyObject UIObj, string parentType)
        {
            DependencyObject parent = new DependencyObject();
            do
            {
                parent = VisualTreeHelper.GetParent(UIObj);
                if (parent.DependencyObjectType.Name == parentType)
                {
                    return parent;
                }
                UIObj = parent;
            } while (UIObj != null);
                       
            return null;
        }
0
 
esc_toe_accountAuthor Commented:
Oops, somehow I started using VisualTreeHelper, instead of LogicalTreeHelper which you suggested. That must explain why the former worked and the latter stopped at the page reference. Never noticed the difference till I posted the response! Regardless, you got me to the right solution so points still seem appropriate.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now