There is a listbox, located in the grid, between two rows. You need to stretch the ListBox the entire length between these rows. And regardless of the number of entries in the ListBox. The whole grid should fit on the screen.

<Grid Name="grdEmployeeFilterAndList"> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition Height="auto"/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <StackPanel x:Name="employeeListStackPanel" Grid.Row="0"> </StackPanel> <ListBox x:Name="lstEmployees" Grid.Row="1" Margin="6,5,8,0" SelectionChanged="lst_SelectionChanged" HorizontalContentAlignment="Stretch" ItemTemplate="{StaticResource PersonDataTemplate}" SnapsToDevicePixels="True" Style="{DynamicResource ListBoxStyle1}"> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}"> <Setter Property="Control.Padding" Value="0"/> <EventSetter Event="MouseDoubleClick" Handler="lstEmployees_DoubleClick"/> <Style.Triggers> <Trigger Property="ListBoxItem.IsSelected" Value="True"> <Setter Property="Control.Background" Value="sc#1.000000, 0.769689, 0.831936, 1.000000" /> </Trigger> </Style.Triggers> </Style> </ListBox.ItemContainerStyle> </ListBox> <StackPanel Orientation="Horizontal" Grid.Row="2" Margin="0,0,0,5" HorizontalAlignment="Center"> </StackPanel> </Grid> 

Listbox

Solution: Solution found: the problem was in the parent elements that stretched the window. I dealt with them + the @Mirdin solution helped (which would have worked as well if not for the parent elements).

Listbox2

  • Where is your code? Without a code it’s hard to guess what you did wrong. - VladD
  • Okay, what should it look like? If the list of 20,000 items, the window should stretch vertically? - VladD
  • The application window itself should not be stretched, if the list is large - it appears scrolled. And this list should occupy all the remaining space between two rows. - Daria
  • Then it is not clear what the @Mirdin answer does not suit you. Then you can show a screenshot of the problem. - VladD
  • On the ListBox screenshot on the left, done as advised by @Mirdin. As a result, the entire list went off the screen, but it’s necessary to stay within, with scrolling, because At the bottom of the list are more buttons. On the right, you can see the scrolling of the entire application window (which should not be natural if ListBox does not get out of borders). In the designer studio everything is OK - ListBox neatly stretched as expected. But when you start it turns out differently. - Daria

2 answers 2

So you tried?

 <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition Height="1*"/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> 

Line 1 will take all free space

  • ListBox contains many items and as a result the list is hidden somewhere far below the screen. - Daria
  • @Daria when using the DockPanel - it will scroll right away. Read another answer - Yurii Manziuk

in my opinion, the use of the grid bit old why not use DockPanel with the LastChildFill= true property?

code example:

 <DockPanel LastChildFill="True"> <Rectangle DockPanel.Dock="Top" Fill="Aquamarine" MinHeight="100"> </Rectangle> <Rectangle DockPanel.Dock="Bottom" Fill="Aqua" MinHeight="100"></Rectangle> <ListBox Name="lisb" Margin="15" BorderThickness="2" BorderBrush="Blue"> </ListBox> </DockPanel> 

Item generation code:

 for(int i = 0; i < 1000; i++) { lisb.Items.Add(new ListBoxItem() { Content = Convert.ToString(i) }); } 

Display example: enter image description here

  • the result is the same as in the previous answer - a long list going nowhere. - Daria
  • @Daria is forced to disagree. Now I’ll update the answer and the screen with automatically generated values - Yurii Manziuk
  • My content is as follows: lstEmployees.ItemsSource = empl.CollectPersons (), which returns a DataView. If you set the ListBox Height to a specific value, then yes, it will scroll. My Grid is also a nested element in a heap of others, apparently there is a problem. - Daria
  • @Daria may be such an option. but still, using the grid is a bad option, as for me - Yurii Manziuk