none
关于ChildWindow的显示居中问题,是BUG吗? RRS feed

  • 问题

  • 一个高度和宽带自动调节的ChildWindow,  放入母控件中,这时是自动居中的。 但当ChildWindow中因为内容变化而长度或宽度加大时,会发生左上角坐标不变,仅向右和向下扩张的情况。除不美观外,还可能使内容落在屏外

    请教,当ChildWindow中因为内容变化时,如何处理才能使它重新居中?

    2013年3月30日 4:06

答案

  • 你好,

    这个不是bug,我们可以根据childwindow的布局改变,重设居中

    建议重写childwindow的onopen事件

    protected override void OnOpened()
    {
    this.LayoutUpdated += new EventHandler(OnChildWindowLayoutUpdated);
    base.OnOpened();
    }
    
    //in the ctor:
    this.Closed += new EventHandler(OnChildWindowClosed);
    
    void OnChildWindowClosed(object sender, EventArgs e)
    {
    this.LayoutUpdated -= OnChildWindowLayoutUpdated;
    }
    
    void OnChildWindowLayoutUpdated(object sender, EventArgs e)
    {
                var root = VisualTreeHelper.GetChild(this, 0) as FrameworkElement;
                if (root != null)
                {
                    var contentRoot = root.FindName("ContentRoot") as FrameworkElement;
    
                    if (contentRoot != null)
                    {
                        var group = contentRoot.RenderTransform as TransformGroup;
    
                        if (group != null)
                            if (group.Children.Count == 6)
                            {
                                var tf1 = group.Children[3] as TranslateTransform;
                                var tf2 = group.Children[5] as TranslateTransform;
    
                                if (tf1 != null)
                                {
                                    tf1.X = 0;
                                    tf1.Y = 0;
                                }
    
                                if (tf2 != null)
                                {
                                    tf2.X = 0;
                                    tf2.Y = 0;
                                }
                            }
                    }
                }
    }

    详细请参考以下博客:

    http://www.codeproject.com/Articles/129329/How-to-Reposition-a-Silverlight-Child-Window

    http://ruifigueiredopt.wordpress.com/2011/02/26/dynamically-re-position-a-childwindow/


    Mark Yu - MSFT
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年4月2日 3:11
    版主