views:

1152

answers:

2

This is a follow up question to an answered question [here][1].

There the startup position of a WPF window was defined in XAML. Now I'm wondering how to change those properties in code? For example could I say something like:

Window1.Top = 40 in the window load event handler? Or which window event would I need to set those for it dynamically alter the starting position?

The goal is to set the windows start position dynamically before it is rendered.

Thanks!

A: 

Personally, I'd throw the "Window1.Top = 40" types of lines into the constructor, after the call to InitializeComponent(). That's sure to be called before the window is actually rendered.

Edit: Oops. I should have read more carefully.

Are you trying to set the window's position from some class other than that of the window itself? My suggestion would work if you're able to set the position of Window1 from within the constructor of Window1.

Otherwise, I would say the best you could do would be to listen to the window's Loaded event and set the position from there.

Skinniest Man
Thanks for your input! Just for curiosities sake, in terms of setting the window's position when it's created from another class, couldn't you just make another constructor with the positioning properties as a parameter and then it would in the same way?
Evan
Yes - that would be the easiest way to do it.
Reed Copsey
Good point. I should have thought of that.
Skinniest Man
A: 

This is fairly 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 wish, but if you're going to set Top/Left, make sure to set WindowStatupLocation (or have it set to manual in XAML).

Reed Copsey