按需刷新 WPF 窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14182829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:38:14  来源:igfitidea点击:

refreshing a WPF window on demand

wpfwindowrefresh

提问by Idov

I noticed that if I move my WPF window through code, I will only see that changes when the window refreshes (if I minimize and then maximize it).
Is it possible to refresh the GUI of a WPF on demand?

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

EDIT: I'm actually trying to make this window follow another window.

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

So I'm hooking to the other window's events:

所以我挂到另一个窗口的事件:

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

and then I change my window's position:

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

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

采纳答案by Clemens

You should add your ParentLocationChangedhandler to the LocationChangedevent instead of LayoutUpdated(which serves a completely different purpose).

您应该将您的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;
}

回答by Matthias

You should hook up parent.LocationChanged and then call InvalidateVisual. See MSDN for more details.

您应该连接 parent.LocationChanged,然后调用 InvalidateVisual。有关更多详细信息,请参阅MSDN