Sean
Sean

Reputation: 346

How do I force my window to re-size?

I have an auto-sized application user control in WPF (Height="Auto" Width="Auto"). When a bunch of elements inside it are resized, the windows stays the same rendered size. How do I force it to resize? I have tried this line Application.Current.MainWindow.SizeToContent = SizeToContent.WidthAndHeight; in the function that resizes the components inside the window. The compiler gives me an error that there is no object associated with this call. I have tried this line MainWindowBorder.Height = Double.NaN; to trick it to resize. No luck either.

Upvotes: 0

Views: 1348

Answers (3)

Sean
Sean

Reputation: 346

To obtain the window of your current user control, see:

Access parent window from User Control

Then use:

Window yourParentWindow = Window.GetWindow(userControl);
yourParentWindow.SizeToContent = SizeToContent.WidthAndHeight;

Upvotes: 1

Kay Lee
Kay Lee

Reputation: 952

If all the inside controls can be resized but the only one thing to resize is Window, How about this simple approach? Hope this helps..

int mycurrentscreenwidth = 1366;
int mycurrentwindowwidth = 1366;

var screen = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
var screenwidth = screen.Width;

if (Convert.ToInt32(screenwidth) < 1366)
{   
    double calculate_scalefactor= Convert.ToInt32(screenwidth) / (double)mycurrentscreenwidth;
    double newwidth_tobescaled = mycurrentwindowwidth * calculate_scalefactor;

    this.Width = newwidth_tobescaled;
}

Upvotes: 1

Related Questions