How to make a WPF ListView no taller than its contents but fit to the window with other controls? How to make a WPF ListView no taller than its contents but fit to the window with other controls? wpf wpf

How to make a WPF ListView no taller than its contents but fit to the window with other controls?


Try

<Grid VerticalAlignment="Top">    <Grid.RowDefinitions>        <RowDefinition Height="auto" />        <RowDefinition Height="*" />        <RowDefinition Height="auto" />    </Grid.RowDefinitions>    <Button Content="hello" />    <ScrollViewer Grid.Row="1" >    <ListView >            <ListBoxItem Content="hi" />            <ListBoxItem Content="hi" />            <ListBoxItem Content="hi" />            <ListBoxItem Content="hi" />            <ListBoxItem Content="hi" />            <!-- Some Items -->    </ListView>    </ScrollViewer>    <Button Content="hello" Grid.Row="2" /></Grid>


Use Grid with three rows and set the height of all three to Auto so that they will size to content.

<Window x:Class="WpfApplicationUnleashed.Window1"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:WpfApplicationUnleashed"        Title="Window1" >    <Grid VerticalAlignment="Top">        <Grid.RowDefinitions>            <RowDefinition Height="Auto"></RowDefinition>            <RowDefinition Height="Auto"></RowDefinition>            <RowDefinition Height="Auto"></RowDefinition>        </Grid.RowDefinitions>        <StackPanel Margin="10" Grid.Row="0" Orientation="Horizontal">            <Button Margin="10">These are top controls</Button>            <Label Margin="10">These are top controls</Label>            <TextBox Margin="10">These are top controls</TextBox>        </StackPanel>        <TreeView Margin="10" Grid.Row="1">            <TreeViewItem Header="Item 1" >                <TreeViewItem Header=" Sub Item 1" />                <TreeViewItem Header=" Sub Item 2" />                <TreeViewItem Header="Sub Item 3" />            </TreeViewItem>            <TreeViewItem Header="Item 2" />            <TreeViewItem Header="Item 3" />            <TreeViewItem Header="Item 4" />        </TreeView>        <StackPanel Margin="10" Grid.Row="2" Orientation="Horizontal">            <Button Margin="10">These are bottom controls</Button>            <Label Margin="10">These are bottom controls</Label>            <TextBox Margin="10">These are bottom controls</TextBox>        </StackPanel>    </Grid></Window>


I struggled with the size, too. And the answer was really to set the Height Property of my Grid Rows. I have the following setup:

<Grid>    <Grid.RowDefinitions>        <RowDefinition Height="*"></RowDefinition>        <RowDefinition Height="Auto"></RowDefinition>    </Grid.RowDefinitions>    <ListBox Grid.Row="0"                         HorizontalAlignment="Stretch"                         VerticalContentAlignment="Stretch"                          ScrollViewer.CanContentScroll="True"                         ScrollViewer.VerticalScrollBarVisibility="Visible"                         ItemsSource="{Binding AuditEntries}"                         Margin="1 0 1 1" BorderThickness="0" VerticalAlignment="Top"/>    <Button Grid.Row="1" /></Grid>

So what really solved my problem was to set the Height property of the first row definition (that contains my ListBox):

<RowDefinition Height="*"></RowDefinition>

What jolly fun...