Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get WPF Window to autosize to content and no more

I have a dialog containing 2 TextBlocks, a Progress Bar and a cancel Button.

Here is the XAML:

<Window x:Class="WpfApplication4.MainWindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     xmlns:local="clr-namespace:WpfApplication4"     mc:Ignorable="d"     Title="MainWindow" Height="Auto" Width="200"> <Grid>     <Grid.RowDefinitions>         <RowDefinition Height="Auto"/>         <RowDefinition Height="Auto"/>         <RowDefinition Height="Auto"/>         <RowDefinition Height="Auto"/>     </Grid.RowDefinitions>      <TextBlock x:Name="txtFirst" Grid.Row="0"  Margin="5" TextWrapping="Wrap">This is a really really really really long string that wraps</TextBlock>     <TextBlock x:Name="txtSecond" Grid.Row="1"  Margin="5" Text="A Shorter string" TextWrapping="Wrap" MaxWidth="200"/>     <ProgressBar x:Name="prgProgress" Grid.Row="2" Margin="5" Height="20" />     <Button x:Name="btnCancel" Grid.Row="3" Margin="5" Height="25" Width="50"/> </Grid> </Window> 

I would like the Window not to have a fixed height but auto adjust its height based on the size of its children and no more, but can’t see a way to do this. At the moment when I don’t assign anything to the Window’s height, it seems to adopt a height that is much bigger that the content.

Not sure why, or where it gets height value from? If I set Windows Height = “Auto” I get the same thing. All the heights for the RowDefinitions are set to “Auto”, which I take to mean ‘set row height to be row child height’.

like image 562
Cleve Avatar asked Oct 18 '16 08:10

Cleve


People also ask

What is WPF SizeToContent?

SizeToContent is automatically set to Manual if a user resizes the window by using the resize grip or dragging the border.

How do I open a WPF window in full screen?

Currently, the only way I know how to fullscreen my wpf application is: WindowStyle=None and WindowState=Maximized (and Topmost=True, though this is just needed to make sure it's above everything else).

How do I center a window in WPF?

Centring Windows To open a new window and have it centred on the screen, set the WindowStartupLocation property to CenterScreen. You can do this using the XAML or in code before the window is displayed.


1 Answers

You need to use SizeToContent property, check the msdn link.

Example:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"         …         SizeToContent="WidthAndHeight"> 
like image 101
flash Avatar answered Sep 20 '22 06:09

flash