refreshing a WPF window on demand

*爱你&永不变心* 提交于 2019-12-11 02:36:17

问题


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?

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

回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/14182829/refreshing-a-wpf-window-on-demand

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!