Dedddeded
Dedddeded

Reputation: 23

UWP: How to adjust window size to its content?

I have a page. I have a grid inside this page.

 <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
 </Grid.RowDefinitions>

In the 2nd row I have a control, which I resize. When control becomes too big for window, I want that window to stretch. How is it possible?

Upvotes: 1

Views: 1293

Answers (2)

Qiu
Qiu

Reputation: 11

For developers who are from WPF to UWP/WinUI.

In principle, it requires that window's size be dependent on its content. While for UWP and WinUI, content will adjust themselves by window's size. This makes sense because window is not the only possible host of Window's content.

For example, the following code will make a window getting smaller and smaller, because it forms a circular dependency.

private void ContentGrid_SizeChanged(object sender, SizeChangedEventArgs e)
{
    this.Width = e.NewSize.Width;
    this.Height = e.NewSize.Height;
}

If you really want to resize to its content, the circle must be cut to prevent infinite resizing. To be specific, you must either

  1. fix your content's size, or
  2. use some other strategy to reach a compromise between window and its content

But in either way, the ui will become much less flexible and error prone to culture change.

Upvotes: 0

Stefan Wick  MSFT
Stefan Wick MSFT

Reputation: 13850

There is no way to author the XAML so that the Window will resize automatically, but you can write code to manage the size of the view/window in your code behind with the ApplicationView.TryResizeView(Size) method.

Upvotes: 2

Related Questions