title: How do you keep the WPF window sized to content with an Expander after resize
body: <p>I've got a WPF window with <code>SizeToContent="Height"</code>. This window contains an <code>&lt;Expander /&gt;</code> that displays a list of recent activity. What I'd like is when the expander is expanded the window grows in size proportionally. When hidden the window again resizes proportionally. If the window is resized the expander and it's contained list view should grow to use the new space. (don't mind the colors there there to help me figure this out):</p>
  
  <p><strong>Normal View</strong></p>
  
  <p><a href="http://www.deploylx.com/so/wpfexpander/Open.png" title="Normal view">alt text http://www.deploylx.com/so/wpfexpander/Open.png</a></p>
  
  <p><strong>Collapsed</strong></p>
  
  <p><a href="http://www.deploylx.com/so/wpfexpander/Closed.png" title="Closed">alt text http://www.deploylx.com/so/wpfexpander/Closed.png</a></p>
  
  <p><strong>Resized to new space</strong></p>
  
  <p><a href="http://www.deploylx.com/so/wpfexpander/Expanded.png" title="Expanded to use new space">alt text http://www.deploylx.com/so/wpfexpander/Expanded.png</a></p>
  
  <p>So far this works great. The problem comes when the <code>&lt;Expander /&gt;</code> is collapsed after the window is resized. Rather than the window collapsing again, the list view is simply hidden:</p>
  
  <p><strong>Collapsed after Resize</strong></p>
  
  <p><a href="http://www.deploylx.com/so/wpfexpander/Collapsed.png" title="Collapsed after Resize">alt text http://www.deploylx.com/so/wpfexpander/Collapsed.png</a></p>
  
  <p>My intuition tells me that the <code>Height</code> of the window is being set when the window resizes and thus overrides the <code>SizeToContent</code> property. So, how can I get the window to keep it's size to content behavior after it's been resized?</p>
  
  <p>Current XAML:</p>
  
  <pre><code>&lt;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"&gt;
  
    &lt;DockPanel&gt;
      &lt;Menu DockPanel.Dock="Top"&gt;
        &lt;MenuItem Header="_File"&gt;
          &lt;MenuItem Header="E_xit" Command="{Binding Path=CloseCmd}" /&gt;
        &lt;/MenuItem&gt;
      &lt;/Menu&gt;
      &lt;Grid DockPanel.Dock="Top" Margin="8" ShowGridLines="True"&gt;
        &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition /&gt;
          &lt;RowDefinition /&gt;
        &lt;/Grid.RowDefinitions&gt;
        &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition /&gt;
          &lt;ColumnDefinition /&gt;
        &lt;/Grid.ColumnDefinitions&gt;
  
        &lt;Grid Grid.RowSpan="2" Grid.Row="0" Grid.Column="0" Margin="0,0,8,0"&gt;
          &lt;Rectangle Fill="Red" /&gt;
          &lt;TextBlock&gt;ActiveCount&lt;/TextBlock&gt;        
        &lt;/Grid&gt;
  
        &lt;Grid Grid.Row="0" Grid.Column="1" Margin="0,0,0,4"&gt;
          &lt;Rectangle Fill="Green" /&gt;
          &lt;TextBlock&gt;Authorization&lt;/TextBlock&gt;
        &lt;/Grid&gt;
        &lt;Grid Grid.Row="1" Grid.Column="1" Margin="0,4,0,0"&gt;
          &lt;Rectangle Fill="Blue" /&gt;
          &lt;TextBlock&gt;Authorization&lt;/TextBlock&gt;
        &lt;/Grid&gt;      
      &lt;/Grid&gt;
      &lt;Expander Header="Recent Activity" Margin="8" IsExpanded="True"&gt;
        &lt;ListView IsSynchronizedWithCurrentItem="True" MinHeight="100"&gt;
          &lt;ListView.View&gt;
            &lt;GridView&gt;
              &lt;GridViewColumn Header="Status"/&gt;
            &lt;/GridView&gt;
          &lt;/ListView.View&gt;
        &lt;/ListView&gt;
      &lt;/Expander&gt;
    &lt;/DockPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>UPDATE:</strong> I've tried listening to the Collapsed event of the expander and resetting the Windows <code>SizeToContent</code> property. This almost works. It will cause it to collapse the window again but when expanded again it goes back to the original 100 pixel height. While it's feasible to store and capture this info it smells hacky and prone to errors.</p>