Dynamically change the starting location of a WPF window

This is the next question to answer the question [here] [1].

There, the starting position of the WPF window in XAML was indicated. Now I am wondering how to change these properties in the code? For example, I can say something like:

Window1.Top = 40 in a window load event handler? Or what window event do I need to set for it to dynamically change the starting position?

The goal is to dynamically set the starting position of Windows before rendering it.

+4
source share
2 answers

This is pretty easy to do in code:

public partial class Window1 { public Window1() { InitializeComponent(); this.Height = 500; this.Width = 500; this.WindowStartupLocation = WindowStartupLocation.Manual; this.Left = 0; this.Top = 0; } } 

You can set any of the parameters you want, but if you are going to set Top / Left, be sure to set WindowStatupLocation (or set it in the manual in XAML).

+7
source

Personally, I would choose the types "Window1.Top = 40" in the constructor after calling InitializeComponent (). This will be called before the actual display of the window.

Edit: Ouch. I should have read carefully.

Are you trying to set the position of a window from some other class than the window itself? My suggestion will work if you can set Window1 position from Window1 constructor.

Otherwise, I would say that the best thing you could do is listen to the "Loaded Event" window and set the position from there.

0
source

All Articles