【问题标题】:How to get the Distance between ChildWindow and Parent Window?如何获取 ChildWindow 和 Parent Window 之间的距离?
【发布时间】:2010-07-09 12:03:14
【问题描述】:

我们可以计算 ChildWindow 和 ParentWidnow 之间的差距吗?

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

虽然我尝试使用以下方法获取距离:

ChildWindow.Margin 

它返回 0,0,0,0

还有其他方法可以获取 ChildWindow 和 ParantWindow 之间的距离吗?

【问题讨论】:

    标签: wpf silverlight silverlight-3.0


    【解决方案1】:

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

    在覆盖Grid 内部是另一个名为“ContentRoot”的网格,其中包含边框、镶边和您的子内容。它的这个Grid 会被拖来拖去,并且会有一个边距。

    使用这个扩展方法:-

    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");
    

    从中获得利润

    【讨论】:

    • 感谢 Anthony,但我无法为我的 ChildWindow myChildWindow 获取方法 Descendents()
    • @Subhen:你已经在你的项目中包含了上面的类,你了解extension方法是如何工作的吗?
    【解决方案2】:

    用你的窗口的 PointToScreen 和 PointFromScreen 方法进行实验。

    PointFromScreen article

    PointToScreen article

    【讨论】:

    • 无法获取 ChildWindow 的 PointFromScreen 和 PointToScreen 方法。我目前正在使用 SL。
    猜你喜欢
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    • 2019-03-30
    • 1970-01-01
    • 2011-05-28
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多