如何使 ListView 在 WPF 中拉伸以填充列的高度?

huangapple go评论66阅读模式
英文:

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

问题

我有一个 ListView 控件。我想让它拉伸以填充整个列的长度。为了实现这一目标,我将 ColumnDefinition 设置为 "*"。我为 ListView 以及其包含的元素设置了 VerticalAlignment 属性为 Stretch

目前,ListView 仅在其中显示数据时才会增大尺寸。

如何使 ListView 无论显示何种数据,都能拉伸到列的尺寸?

这是我的XAML文件的示例版本。它显示了 ListView 和所有包含元素的 VerticalAlignment 都被设置为 Stretch

<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"/>
英文:

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?

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"/>

答案1

得分: 1

避免在希望内部元素填充外部空间时使用 StackPanel。改用 Grid 并使用 Grid.ColumnDefinitionsGrid.RowDefinitions 来调整位置。

英文:

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.

huangapple
  • 本文由 发表于 2023年3月31日 23:28:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定