按需刷新 WPF 窗口 [英] refreshing a WPF window on demand

查看:39
本文介绍了按需刷新 WPF 窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我实际上是想让这个窗口跟随另一个窗口.

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

推荐答案

您应该将 ParentLocationChanged 处理程序添加到 LocationChanged 事件而不是 LayoutUpdated(用于完全不同的目的).

You should add your ParentLocationChanged handler to the LocationChanged event instead of LayoutUpdated (which serves a completely different purpose).

m_wndParent.LocationChanged += ParentLocationChanged;

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

这篇关于按需刷新 WPF 窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆