Resource

Automatically resize a Window to fit content in WPF

XAML Designer Resources Jan 13, 2010
Automatically resize a Window to fit content in WPF

SizeToContent property of Window indicates whether a window will automatically size itself to fit the size of its content. This value is a type of SizeToContent enumeration that has four values – Manual, Width, Height, and WidthAndHeight.

  • When SizeToContent is set to WidthAndHeight, the values of Height or Width properties have no effect.
  • When SizeToContent is set to Height, setting Height does not change the height of the window.
  • When SizeToContent is set to Width, setting Width does not change the width of the window.
  • When SizeToContent ise set to Manual, the size of window is determined by other properties including Width, Height, MaxWidth, MaxHeight, MinWidth, and MinHeight.

The following code snippet sets the SizeToContent to WidthAndHeight.

<Window x:Class="WindowSample.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="WPF Window Sample"

        Width="300" Height="200" Left="500" Top="500"

        SizeToContent="WidthAndHeight" >


This example shows how to set the SizeToContent property to specify how a window resizes to fit its content at run-time. Choose one of them ;)

// Manually alter window height and width
this.SizeToContent = SizeToContent.Manual;

// Automatically resize width relative to content
this.SizeToContent = SizeToContent.Width;

// Automatically resize height relative to content
this.SizeToContent = SizeToContent.Height;

// Automatically resize height and width relative to content
this.SizeToContent = SizeToContent.WidthAndHeight;
post comment
     

very nice article. Thanks

Posted by Santosh Yadav Mar 19, 2013

Hey man, its very cool

Posted by masod Aug 04, 2012

Can you show how to automaticly resize content to fit window?

Posted by drsullivan Jan 26, 2011
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts