How to save WPF window size to content using Expander after resizing

I have a WPF window with SizeToContent="Height" . This window contains <Expander /> , which displays a list of recent activities. I would like the extension of the expander to increase the window size proportionally. When hiding, the window again proportionally changes. If the window is resized, the expander and it must contain a list in order to use the new space. (not against the colors there to help me figure this out):

Normal view

alt text http://www.deploylx.com/so/wpfexpander/Open.png

Collapsed

alt text http://www.deploylx.com/so/wpfexpander/Closed.png

Changed to a new space

alt text http://www.deploylx.com/so/wpfexpander/Expanded.png

So far, this works great. The problem occurs when <Expander /> crashes after resizing the window. Instead of the window crashing down again, the list view is simply hidden:

Resets after resizing

alt text http://www.deploylx.com/so/wpfexpander/Collapsed.png

My intuition tells me that the Height window of the window is set when the window is resized and thus overrides the SizeToContent property. So, how can I get a window to preserve its size for the behavior of the content after changing it?

Current XAML:

 <Window x:Class="DeployLX.Licensing.Nlm.Admin.v3.DashboardWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Dashboard" Width="504" SizeToContent="Height" Height="275"> <DockPanel> <Menu DockPanel.Dock="Top"> <MenuItem Header="_File"> <MenuItem Header="E_xit" Command="{Binding Path=CloseCmd}" /> </MenuItem> </Menu> <Grid DockPanel.Dock="Top" Margin="8" ShowGridLines="True"> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid Grid.RowSpan="2" Grid.Row="0" Grid.Column="0" Margin="0,0,8,0"> <Rectangle Fill="Red" /> <TextBlock>ActiveCount</TextBlock> </Grid> <Grid Grid.Row="0" Grid.Column="1" Margin="0,0,0,4"> <Rectangle Fill="Green" /> <TextBlock>Authorization</TextBlock> </Grid> <Grid Grid.Row="1" Grid.Column="1" Margin="0,4,0,0"> <Rectangle Fill="Blue" /> <TextBlock>Authorization</TextBlock> </Grid> </Grid> <Expander Header="Recent Activity" Margin="8" IsExpanded="True"> <ListView IsSynchronizedWithCurrentItem="True" MinHeight="100"> <ListView.View> <GridView> <GridViewColumn Header="Status"/> </GridView> </ListView.View> </ListView> </Expander> </DockPanel> </Window> 

UPDATE:. I tried to listen to the expander's Collapsed event and reset the Windows SizeToContent property. It almost works. This will force it to minimize the window again, but as it expands, it returns to its original height of 100 pixels. Although it is possible to store and capture this information, it smells like hacks and is prone to errors.

+49
wpf
May 08 '09 at 9:00
source share
5 answers

You must make your window immutable if you intend to use SizeToContent. In addition, you should not use SizeToContent = "Height", and then set the explicit height. Think about it - what should WPF assume for window height, user preferences, or content? He can't just switch between them.

+55
Jun 11 '09 at 22:26
source share

The easiest way to handle this is to manually resize from the equation by setting ResizeMode="NoResize" in the window. However , if you have WindowStyle="None" , I noticed that on Vista Aero this causes the window to completely clear "chrome" and the window looks uncomfortable. It's also a bit of a cop since it looks like you want to give users the ability to resize.

The problem is that you have two conflicting goals: 1.) you always want SizeToContent="Height" when changing the expander control, 2.) you want SizeToContent="Height" when expanding the expander control , if the user manually changed window size (SizeToContent = "Manual"), in which case you would like to return to the height of the user manual.

Clap. I don’t think you can get around preserving the increased height yourself. WPF will not remember the height of the user guide when expanding as soon as you return to SizeToContent="Height" in the processed event handler.

+10
Jul 17 '09 at 11:30
source share

Try this, it should fit your needs: SizeToContent = "WidthAndHeight" Height = "Auto" Width = "Auto"

+2
Jul 15 '16 at 9:52
source share

As I found out in my question , setting Height to Double.NaN, it brings the reset happiness of SizeToContent. I do not know if he remembers the size of your expander. You can try Kent Resizer to move the size behavior to the expander, and not to the containing window.

+1
May 09 '09 at 4:51 a.m.
source share

I found that putting the body of my window into a view, and then putting the view as the only child in the window, solved similar problems ...

Not the most desirable solution, but it works.

+1
Jun 11 '09 at 22:17
source share



All Articles