按需刷新WPF窗口

时间:2013-01-06 13:59:17

标签: wpf window refresh

我注意到如果我通过代码移动WPF窗口,我只会在窗口刷新时看到更改(如果我最小化然后最大化)。
是否可以按需刷新WPF的GUI?

编辑:我实际上是想让这个窗口跟随另一个窗口。

所以我正在接触其他窗口的事件:

    m_wndParent = parent;
    this.Owner = m_wndParent;
    m_wndParent.SizeChanged += ParentSizeChanged;
    m_wndParent.LayoutUpdated += ParentLocationChanged;

然后我改变了我的窗口位置:

   private void ParentLocationChanged(object sender, EventArgs e)
        {
            Window parent = sender as Window;
            this.Top = parent.Top;
            this.Left = parent.Left;
            this.UpdateLayout();
        }

2 个答案:

答案 0 :(得分:1)

您应该将ParentLocationChanged处理程序添加到LocationChanged事件而不是LayoutUpdated(其用途完全不同)。

m_wndParent.LocationChanged += ParentLocationChanged;

private void ParentLocationChanged(object sender, EventArgs e)
{
    var parent = sender as Window;
    Top = parent.Top;
    Left = parent.Left;
}

答案 1 :(得分:0)

您应该挂接parent.LocationChanged,然后调用InvalidateVisual。请参阅MSDN for more details