Cheetah
Cheetah

Reputation: 14379

Get size of Window before Show()

I need to use the Width and Height properties of a Window to determine where on the screen it should be displayed.

Of course these aren't available until you actually draw the Window (via the Window.Show() method).

Now my current hack/workaround is to set the Top and Left properties to -9999, Show() and then reposition.

I am wondering if there is a non hacky way of doing this?

(On a side note, having looked at the documentation, I should probably be using the ActualWidth and ActualHeight properties)

Upvotes: 2

Views: 1207

Answers (4)

ogi
ogi

Reputation: 51

You can use the Loaded event of the target Window. In the handler, both the ActualWidth/ActualHeight and Width/Height are valid.

Window wnd; //...
wnd.Loaded += (object sender, RoutedEventArgs e) =>
{
    // Set wnd.Top and wnd.Left here.
};
wnd.ShowDialog();

Upvotes: 4

drankin2112
drankin2112

Reputation: 4804

I'm missing something here. You can just set the Location and Size properties in the IDE. You can also set them programmatically before Show().

Upvotes: 0

MaMazav
MaMazav

Reputation: 1885

Depends on what you actually try to do, you can use the layout system:

http://msdn.microsoft.com/en-us/library/ms745058%28v=vs.110%29.aspx

For example you could override MeasureOverride or ArrangeOverride to detect new size during layouting.

Upvotes: 0

devavx
devavx

Reputation: 1045

This might sound silly but is worth a shot;

Set Window's opacity property to 0% then show the Window and calculate the dimensions.

Upvotes: 0

Related Questions