代码之家  ›  专栏  ›  技术社区  ›  Simsons

如何获取子窗口和父窗口之间的距离?

  •  2
  • Simsons  · 技术社区  · 14 年前

    我们能计算一下Bettwen ChildWindow和ParentWidNow之间的差距吗?

    虽然我可以拖动到ChildWindow周围,但从顶部和左侧有一段距离。

    当我试着用以下方法计算距离时:

    ChildWindow.Margin 
    

    它返回0,0,0,0

    有没有其他方法可以让你的距离超过儿童窗和偏执狂?

    2 回复  |  直到 7 年前
        1
  •  1
  •   AnthonyWJones    14 年前

    子窗口控件实际上占据了Silverlight内容窗口上的所有可用空间。模板中的第一个元素是覆盖 Grid . 它的网格使“父”窗口内容变暗,并使用所有鼠标事件,否则将转到“父”内容。

    在覆盖层内 网格 是另一个称为“ContentRoot”的网格,其中包含边框、Chrome和您的子内容。其这 网格 这会被拖来拖去,而且会有一定的利润。

    使用此扩展方法:

    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;  
         }  
       }  
    }
    

    您可以在以下位置找到ContentRoot:

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

    从中你可以得到利润

        2
  •  0
  •   Eugene Cheverda    14 年前

    尝试使用Windows的PointToScreen和PointFromScreen方法。

    PointFromScreen article

    PointToScreen article