英文:
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.ColumnDefinitions 或 Grid.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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论