VIBlend
// Client Login
Home Products Downloads Purchase Support Forum About Blog

Welcome Guest! To enable all features please try to register or login.
Find the parent Window of a WPF control
viblendsupport
#1 Posted : Thursday, March 10, 2011 10:50:41 AM(UTC)
Rank: Advanced Member

Posts: 372

There are scenarios when you need to find the control's parent window. In such situations, you can use the VisualTreeHelper class.

The example code below shows how to get the parent window of any WPF element or control:

C#
Code:
   
public static Window GetParentWindow(DependencyObject child)
{
     DependencyObject parentObject = VisualTreeHelper.GetParent(child);

     if (parentObject == null)
     {
        return null;
     }

     Window parent = parentObject as Window;
     if (parent != null)
     {
        return parent;
     }
     else
     {
        return GetParentWindow(parentObject);
     }
}


VB .NET
Code:
Public Shared Function GetParentWindow(ByVal child As DependencyObject) As Window
     Dim parentObject As DependencyObject = VisualTreeHelper.GetParent(child)

     If parentObject Is Nothing Then
        Return Nothing
     End If

     Dim parent As Window = TryCast(parentObject, Window)
     If parent IsNot Nothing Then
        Return parent
     Else
        Return GetParentWindow(parentObject)
     End If
End Function
Copyright © 2014 VIBlend  
ALL RIGHTS RESERVED  
 
Terms of Use | Privacy Policy
Silverlight Controls WinForms Controls Knowledge Base About Us
       
Silvelright DataGrid Ribbon Interface Technical Support Blog
Silverlight Editors WinForms DataGrid
Silverlight Ribbon Bar Calendar Downloads Forum
Navigation Bars Navigation Bars    
Menu & ContextMenu TreeView Purchase Online Client Login
ComboBox & ListBox Tab Control    
Silverlight Pivot Table WinForms Pivot Grid Request a feature Report a bug