2

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();
        }
Idov
  • 5,006
  • 17
  • 69
  • 106

2 Answers2

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;
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
0

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

Matthias
  • 5,574
  • 8
  • 61
  • 121
  • InvalidateVisual isn't necessary. It's just setting the Left and Top properties. – Clemens Jan 06 '13 at 15:12
  • 1
    He asked "Is it possible to refresh the GUI of a WPF on demand?" If so, he needs InvalidateVisual. If he just wants to set the position so that the window follows it's parent, then only Left/Top is necessary. – Matthias Jan 06 '13 at 15:16
  • He has edited the question. Obviously he just wants to move the window. It is important here to distinguish between refreshing (i.e. UpdateLayout or InvalidateVisual) and moving the desktop window. These two are completely unrelated and you should at least point that out in your answer. – Clemens Jan 06 '13 at 15:22