Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I make a ListView stretch to fill the height of a column in WPF?

I have a ListView control. I want to make it stretch to fill the entire length of a column. To achieve this, I set the ColumnDefinition to "*". I set the ListView‘s VerticalAlignment property to Stretch, both for the ListView and for its containing elements.

Currently, the ListView will only increase in size when data is shown inside of it.

How do I make the ListView stretch to the size of the column no matter what data is shown inside it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is a sample version of my XAML file. It shows that the VerticalAllignment is set to stretch on the ListView and all of the containing elements:

<Grid>
    <TabControl>
        <TabItem Header="Main" 
                    Margin="1,-1,-2,1">
            <Grid Background="#FFE5E5E5" >

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                    
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <Border BorderBrush="Black" 
                        BorderThickness=".5" 
                        Padding="4"
                        Grid.Column="0"
                        VerticalAlignment="Stretch"
                        HorizontalAlignment="Stretch">

                    <StackPanel Orientation="Vertical" 
                                VerticalAlignment="Stretch" 
                                HorizontalAlignment="Stretch" >

                        <ListView x:Name="FilesDisplayed"  
                                    ItemsSource="{Binding AllFilePaths}" 
                                    SelectionChanged="FilesDisplayed_SelectionChanged" 
                                    HorizontalAlignment="Stretch"
                                    VerticalAlignment="Stretch"
                                    ScrollViewer.HorizontalScrollBarVisibility="Visible"
                                    ScrollViewer.VerticalScrollBarVisibility="Visible"/>

>Solution :

Avoid using StackPanel when you want inner elements to fill the outer space. Replace it with Grid and use Grid.ColumnDefinitions or Grid.RowDefinitions to adjust the location.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading