Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't get WPF Listview to collapse when expanders in the listview collapse
    text
    copied!<p>I have a wpf <code>ListView</code> with two <code>Expander</code>s in it. When the expanders expand the listview makes room for the content. When they collapse the listview does not "take back" that extra space. I have set <code>HorizontalAlignment</code> and <code>VerticalAlignment</code> to <code>Stretch</code> everywhere. Is there a way to do this? The <code>Details</code> and <code>Chart</code> columns contain the expanders. See code below. Thanks. </p> <p>Here at</p> <pre><code>&lt;Window.Resources&gt; &lt;DataTemplate x:Key="StockPriceChangeCell"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;Label Name="stockPriceChangeValue" Foreground="{Binding StockPriceChangeColor}" Content="{Binding StockPriceChange}" Width="Auto"&gt;&lt;/Label&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="DetailsCell"&gt; &lt;StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&gt; &lt;Expander Header="Details..." BorderBrush="DarkBlue" Width="200" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&gt; &lt;Grid Name="stockDetailGrid" Background="Beige" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ShowGridLines="False"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="96*" /&gt; &lt;RowDefinition Height="6*" /&gt; &lt;RowDefinition Height="96*" /&gt; &lt;RowDefinition Height="6*" /&gt; &lt;RowDefinition Height="96*" /&gt; &lt;RowDefinition Height="6*" /&gt; &lt;RowDefinition Height="96*" /&gt; &lt;RowDefinition Height="6*" /&gt; &lt;RowDefinition Height="96*" /&gt; &lt;RowDefinition Height="96*" /&gt; &lt;RowDefinition Height="96*" /&gt; &lt;RowDefinition Height="6*" /&gt; &lt;RowDefinition Height="96*" /&gt; &lt;RowDefinition Height="6*" /&gt; &lt;RowDefinition Height="96*" /&gt; &lt;RowDefinition Height="6*" /&gt; &lt;RowDefinition Height="96*" /&gt; &lt;RowDefinition Height="6*" /&gt; &lt;RowDefinition Height="96*" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="Auto" /&gt; &lt;ColumnDefinition Width="Auto" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Label Grid.Row="0" Grid.Column="0" Name="previousClosePrompt" FontWeight="Bold" FontSize="12" Height="28" Width="Auto"&gt;Prev Close:&lt;/Label&gt; &lt;Label Grid.Row="0" Grid.Column="1" Name="previousCloseValue" FontWeight="Bold" FontSize="12" Height="28" Width="Auto" Content="{Binding StockDetails.PreviousClose}"&gt;&lt;/Label&gt; &lt;Separator Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Name="stockSeparator3" Margin="0,2" /&gt; &lt;Label Grid.Row="2" Grid.Column="0" Name="openPricePrompt" FontWeight="Bold" FontSize="12" Height="28"&gt;Open:&lt;/Label&gt; &lt;Label Grid.Row="2" Grid.Column="1" Name="openPriceValue" FontWeight="Bold" FontSize="12" Height="28" Content="{Binding StockDetails.OpeningPrice}"&gt;&lt;/Label&gt; &lt;Separator Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Name="stockSeparator5" Margin="0,2" /&gt; &lt;Label Grid.Row="4" Grid.Column="0" Name="yearlyTargetEstimatePrompt" FontWeight="Bold" FontSize="12" Height="28"&gt;1y Target Est:&lt;/Label&gt; &lt;Label Grid.Row="4" Grid.Column="1" Name="yearlyTargetEstimateValue" FontWeight="Bold" FontSize="12" Height="28" Content="{Binding StockDetails.YearTargetEstimate}"&gt;&lt;/Label&gt; &lt;Separator Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" Name="stockSeparator6" Margin="0,2" /&gt; &lt;Label Grid.Row="6" Grid.Column="0" Name="stockDaysRangePrompt" FontWeight="Bold" FontSize="12" Height="28"&gt;Day's Range:&lt;/Label&gt; &lt;Label Grid.Row="6" Grid.Column="1" Name="stockDaysRangeValue" FontWeight="Bold" FontSize="12" Height="28" Content="{Binding StockDetails.DaysRange}"&gt;&lt;/Label&gt; &lt;Separator Grid.Row="7" Grid.Column="0" Grid.ColumnSpan="2" Name="stockSeparator7" Margin="0,2" /&gt; &lt;Label Grid.Row="8" Grid.Column="0" Name="stockYearsRangePrompt" FontWeight="Bold" FontSize="12" Height="28"&gt;52wk Range:&lt;/Label&gt; &lt;Label Grid.Row="8" Grid.Column="1" Name="stockYearsRangeValue" FontWeight="Bold" FontSize="12" Height="28" Content="{Binding StockDetails.YearsRange}"&gt;&lt;/Label&gt; &lt;Separator Grid.Row="9" Grid.Column="0" Grid.ColumnSpan="2" Name="stockSeparator8" Margin="0,2" /&gt; &lt;Label Grid.Row="10" Grid.Column="0" Name="averageVolumePrompt" FontWeight="Bold" FontSize="12" Height="28"&gt;Avg Vol (3m):&lt;/Label&gt; &lt;Label Grid.Row="10" Grid.Column="1" Name="averageVolumeValue" FontWeight="Bold" FontSize="12" Height="28" Content="{Binding StockDetails.AverageVolume}"&gt;&lt;/Label&gt; &lt;Separator Grid.Row="11" Grid.Column="0" Grid.ColumnSpan="2" Name="stockSeparator9" Margin="0,2" /&gt; &lt;Label Grid.Row="12" Grid.Column="0" Name="marketCapPrompt" FontWeight="Bold" FontSize="12" Height="28"&gt;Market Cap:&lt;/Label&gt; &lt;Label Grid.Row="12" Grid.Column="1" Name="marketCapValue" FontWeight="Bold" FontSize="12" Height="28" Content="{Binding StockDetails.MarketCap}"&gt;&lt;/Label&gt; &lt;Separator Grid.Row="13" Grid.Column="0" Grid.ColumnSpan="2" Name="stockSeparator10" Margin="0,2" /&gt; &lt;Label Grid.Row="14" Grid.Column="0" Name="priceEarningRatioPrompt" FontWeight="Bold" FontSize="12" Height="28"&gt;P/E (ttm):&lt;/Label&gt; &lt;Label Grid.Row="14" Grid.Column="1" Name="priceEarningRatioValue" FontWeight="Bold" FontSize="12" Height="28" Content="{Binding StockDetails.PriceEarningsRatio}"&gt;&lt;/Label&gt; &lt;Separator Grid.Row="15" Grid.Column="0" Grid.ColumnSpan="2" Name="stockSeparator11" Margin="0,2" /&gt; &lt;Label Grid.Row="16" Grid.Column="0" Name="earningsPerSharePrompt" FontWeight="Bold" FontSize="12" Height="28"&gt;EPS (ttm):&lt;/Label&gt; &lt;Label Grid.Row="16" Grid.Column="1" Name="earningsPerShareValue" FontWeight="Bold" FontSize="12" Height="28" Content="{Binding StockDetails.EarningsPerShare}"&gt;&lt;/Label&gt; &lt;Separator Grid.Row="17" Grid.Column="0" Grid.ColumnSpan="2" Name="stockSeparator12" Margin="0,2" /&gt; &lt;Label Grid.Row="18" Grid.Column="0" Name="dividendYieldPrompt" FontWeight="Bold" FontSize="12" Height="28"&gt;Div &amp;amp; Yield:&lt;/Label&gt; &lt;Label Grid.Row="18" Grid.Column="1" Name="dividendYieldValue" FontWeight="Bold" FontSize="12" Height="28" Content="{Binding StockDetails.DividendYield}"&gt;&lt;/Label&gt; &lt;/Grid&gt; &lt;/Expander&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="ChartCell"&gt; &lt;StackPanel&gt; &lt;Expander Header="Chart..." BorderBrush="DarkBlue" Width="200" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&gt; &lt;Image Name="stockGraph" Stretch="None" Source="{Binding StockChart}"/&gt; &lt;/Expander&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/Window.Resources&gt; </code></pre> <p>Here is the listview:</p> <pre><code> &lt;TabItem Name="myStocksTab" Header="My Stocks" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&gt; &lt;StackPanel Name="myStocksTabContainerPanel" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&gt; &lt;Label Name="myStocksPrompt" Foreground="GhostWhite" Background="DarkBlue" FontSize="16" FontWeight="Bold"&gt;My Stocks&lt;/Label&gt; &lt;ListView Name="stocksUIList" ItemsSource="{Binding}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch"&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Header="Action" CellTemplate="{StaticResource ButtonsCell}" Width="Auto"/&gt; &lt;GridViewColumn Header="Company/Stock Symbol" Width="Auto" DisplayMemberBinding="{Binding CompanyName}"/&gt; &lt;GridViewColumn Header="Last Trade" Width="Auto" DisplayMemberBinding="{Binding LastTrade}"/&gt; &lt;GridViewColumn Header="Trade Time" Width="Auto" DisplayMemberBinding="{Binding StockDetails.TradeTime}"/&gt; &lt;GridViewColumn Header="Change" CellTemplate="{StaticResource StockPriceChangeCell}"/&gt; &lt;GridViewColumn Header="Volume" Width="Auto" DisplayMemberBinding="{Binding TradingVolume}"/&gt; &lt;GridViewColumn Header="Details" CellTemplate="{StaticResource DetailsCell}" Width="215"/&gt; &lt;GridViewColumn Header="Chart" CellTemplate="{StaticResource ChartCell}" Width="215"/&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; &lt;Button Name="addStock" FontSize="14" FontWeight="Bold" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" Click="AddCompanyStock"&gt;Add Stock...&lt;/Button&gt; &lt;/StackPanel&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload