1

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?

1

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.