Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get WPF ListBox to always be the height of 4 grid rows?
    text
    copied!<p>How can i get my listbox to be the standard height of my rows, even after i add several items to the listbox? I don't want to set a Height on either. I've tried setting some shared size groups, tried binding the height of each of the rows to a different row, etc with no solution in sight. I want the height of the four rows the listbox takes up to be the same height as they are originally. The listbox currently grows to fit all the items in it, therefore making the rows taller, which i don't want. I want the listbox to just get scrollbars if it is higher than 4 "normal" row heights. If i MUST set a maxHeight on the listbox, how can i tell it to be 4x the height of one of my other rows?</p> <p>XAML:</p> <pre><code>&lt;Window x:Class="SharedSizes.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;Border Margin="10" &gt; &lt;StackPanel Grid.IsSharedSizeScope="True"&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="Auto" SharedSizeGroup="A"/&gt; &lt;ColumnDefinition Width="Auto" SharedSizeGroup="B"/&gt; &lt;ColumnDefinition Width="Auto" SharedSizeGroup="C"/&gt; &lt;ColumnDefinition Width="Auto" SharedSizeGroup="D"/&gt; &lt;ColumnDefinition Width="Auto" SharedSizeGroup="E"/&gt; &lt;ColumnDefinition Width="Auto" SharedSizeGroup="F"/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Name="Row1" Height="Auto" SharedSizeGroup="Width"/&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;!--Left Side Parts--&gt; &lt;Label Content="Vendor:" Grid.Row="0" Grid.Column="0"/&gt; &lt;ComboBox Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="4" /&gt; &lt;Label Content="Billing Model:" Grid.Row="1" Grid.Column="0" /&gt; &lt;ComboBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Width="125"/&gt; &lt;Label Content="Billing Cycle:" Grid.Row="2" Grid.Column="0"/&gt; &lt;ComboBox Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" /&gt; &lt;StackPanel Grid.Row="2" Grid.Column="3" Orientation="Horizontal" HorizontalAlignment="Left"&gt; &lt;Button Content="1"/&gt; &lt;Button Content="2"/&gt; &lt;Button Content="3"/&gt; &lt;Button Content="4"/&gt; &lt;/StackPanel&gt; &lt;Label Content="Clients" Grid.Row="2" Grid.Column="5"/&gt; &lt;/Grid&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;!--&lt;RowDefinition Height="{Binding ElementName=Row1, Path=ActualHeight}"/&gt; &lt;RowDefinition Height="{Binding ElementName=Row1, Path=ActualHeight}" /&gt; &lt;RowDefinition Height="{Binding ElementName=Row1, Path=ActualHeight}" /&gt; &lt;RowDefinition Height="{Binding ElementName=Row1, Path=ActualHeight}" /&gt;--&gt; &lt;RowDefinition SharedSizeGroup="Width"/&gt; &lt;RowDefinition SharedSizeGroup="Width"/&gt; &lt;RowDefinition SharedSizeGroup="Width"/&gt; &lt;RowDefinition SharedSizeGroup="Width"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="Auto" SharedSizeGroup="A"/&gt; &lt;ColumnDefinition Width="Auto" SharedSizeGroup="B"/&gt; &lt;ColumnDefinition Width="Auto" SharedSizeGroup="C"/&gt; &lt;ColumnDefinition Width="Auto" SharedSizeGroup="D"/&gt; &lt;ColumnDefinition Width="Auto" /&gt; &lt;ColumnDefinition /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Label Content="Fred" Grid.Row="0"/&gt; &lt;Label Content="Fred" Grid.Row="0" Grid.Column="1"/&gt; &lt;Label Content="Fred" Grid.Row="0" Grid.Column="2"/&gt; &lt;Label Content="Fred" Grid.Row="0" Grid.Column="3"/&gt; &lt;Label Content="Fred" Grid.Row="0" Grid.Column="4"/&gt; &lt;Label Content="Fred" Grid.Row="1"/&gt; &lt;Label Content="Fred" Grid.Row="2"/&gt; &lt;Label Content="Fred" Grid.Row="3"/&gt; &lt;ListBox Name="FredBox" Grid.Row="0" Grid.Column="5" Grid.RowSpan="4" Grid.IsSharedSizeScope="True"/&gt; &lt;/Grid&gt; &lt;/StackPanel&gt; </code></pre> <p> </p> <p>How I want it to look always (and how it does with no items in the listbox):</p> <p><img src="https://i.stack.imgur.com/i6TYR.png" alt="enter image description here"></p> <p>What it does after i added items to the listbox:</p> <p><img src="https://i.stack.imgur.com/k5VqH.png" alt="enter image description here"></p> <p>EDITED FOR VIV's SUGGESTIONS:</p> <pre><code>&lt;Window x:Class="SharedSizes.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;Window.Resources&gt; &lt;!--&lt;Style TargetType="{x:Type RowDefinition}"&gt; &lt;Setter Property="Height" Value="25"/&gt; &lt;/Style&gt;--&gt; &lt;/Window.Resources&gt; &lt;Border Margin="10" &gt; &lt;StackPanel&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;/Grid.RowDefinitions&gt; &lt;!--Left Side Parts--&gt; &lt;Label Content="Vendor:" Grid.Row="0" Grid.Column="0" /&gt; &lt;ComboBox Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="3" /&gt; &lt;Label Content="Billing Model:" Grid.Row="1" Grid.Column="0" /&gt; &lt;ComboBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" /&gt; &lt;Label Content="Billing Cycle:" Grid.Row="2" Grid.Column="0"/&gt; &lt;ComboBox Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" /&gt; &lt;StackPanel Grid.Row="2" Grid.Column="3" Orientation="Horizontal" &gt; &lt;Button Content="1"/&gt; &lt;Button Content="2"/&gt; &lt;Button Content="3"/&gt; &lt;Button Content="4"/&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="{Binding ElementName=SomeLabel, Path=DesiredSize.Height}"/&gt; &lt;RowDefinition Height="{Binding ElementName=SomeLabel, Path=DeisredSize.Height}"/&gt; &lt;RowDefinition Height="{Binding ElementName=SomeLabel, Path=DeisredSize.Height}"/&gt; &lt;RowDefinition Height="{Binding ElementName=SomeLabel, Path=DeisredSize.Height}"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Label Content="Fred" Grid.Row="0" x:Name="SomeLabel"/&gt; &lt;Label Content="Fred" Grid.Row="0" Grid.Column="1"/&gt; &lt;Label Content="Fred" Grid.Row="0" Grid.Column="2" /&gt; &lt;Label Content="Fred" Grid.Row="0" Grid.Column="3"/&gt; &lt;Label Content="Fred" Grid.Row="0" Grid.Column="4"/&gt; &lt;Label Content="Fred" Grid.Row="1"/&gt; &lt;Label Content="Fred" Grid.Row="2"/&gt; &lt;Label Content="Fred" Grid.Row="3"/&gt; &lt;ListView Name="FredBox" Grid.Row="0" Grid.Column="5" Grid.RowSpan="4" /&gt; &lt;/Grid&gt; &lt;/StackPanel&gt; &lt;/Border&gt; </code></pre> <p></p> <p><img src="https://i.stack.imgur.com/Bz974.png" alt="Using Viv&#39;s suggestions"></p>
 

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