Search code examples
wpfsilverlightsilverlight-3.0

How to get the Distance between ChildWindow and Parent Window?


Can we calculate the gap bettwen ChildWindow and ParentWidnow?

While I can Drag arround the ChildWindow there is a Distance from the Top and Left.

WHile I try to get the Distance using:

ChildWindow.Margin 

it is returning 0,0,0,0

Is there any other method to get the Distance bettwen ChildWindow and ParantWindow?


Solution

  • A child window control actually occupies all the available space on the Silverlight content window. The first element in the template is the overlay Grid. Its this Grid which dims the "parent" window content and consumes all the mouse events that would otherwise go to "parent" content.

    Inside the Overlay Grid is another Grid called "ContentRoot" which contains the border, chrome and your child content. Its this Grid that gets dragged around and will have a Margin.

    Using this extension method:-

    public static class VisualTreeEnumeration  
    {  
       public static IEnumerable<DependencyObject> Descendents(this DependencyObject root)  
       {  
         int count = VisualTreeHelper.GetChildrenCount(root);  
         for (int i=0; i < count; i++)  
         {  
           var child = VisualTreeHelper.GetChild(root, i);  
           yield return child;  
           foreach (var descendent in Descendents(child))  
             yield return descendent;  
         }  
       }  
    }
    

    You could find the ContentRoot with:-

    Grid contentRoot = myChildWindow.Descendents().OfType<Grid>().Where(g => g.Name == "ContentRoot");
    

    From which you get the margin