Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,

I'm now working on a window which has to adapts to the space occupied by its content automatically.
I know how to do this in the C# code-behind, but I would like to know if there is another way which avoid the use of C# code-behind.


---Idea for Code-behind:
Measure each child (they are in a stackpanel) ActualHeight and sum them in order to find the height for my window. Then find the Greater width and set that to the window width.


Thanks a lot for any help!!

Jymmy097
Posted

WPF: A Beginner's Guide - Part 1 of n[^] has some useful information about stacking objects.
 
Share this answer
 
have you tried using SizeToContent property of window...

Use like this

HTML
<window x:class="WindowSample.MainWindow" xmlns:x="#unknown">

        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" >
</window>


check this

http://wpf.2000things.com/2011/04/07/269-automatically-sizing-a-window-to-fit-its-contents/
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900