[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

WPF C# Find Window Instance

Posted on 2010-09-03
6
Medium Priority
?
2,014 Views
Last Modified: 2013-11-12
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
Comment
Question by:esc_toe_account
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 8

Accepted Solution

by:
Gururaj Badam earned 1000 total points
ID: 33598571
((Button)sender).Owner should give you the instance of the owner?

Other ways try LogicalTreeHelper.GetParent(sender as DependencyObject);
0
 

Author Comment

by:esc_toe_account
ID: 33598989
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
 

Author Comment

by:esc_toe_account
ID: 33599168
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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 16

Expert Comment

by:SriVaddadi
ID: 33600699
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
 

Author Closing Comment

by:esc_toe_account
ID: 33617674
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
 

Author Comment

by:esc_toe_account
ID: 33617721
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question