我是 WPF 新手。我的同事创建了一个 ListView ,它的高度随着项目的数量而变化,甚至超出了窗口的边界,这样滚动条就不会出现,下面的项目也无法访问。
如何将视图的高度绑定到窗口,以显示滚动条?
我尝试如此设置
VerticalContentAligment = !stretch
,但这不起作用。
到目前为止的代码:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<Style
x:Key="RightTitleText" TargetType="GridViewColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Right" />
</Style>
</UserControl.Resources>
<Grid Loaded="Grid_Loaded">
<DockPanel>
<DockPanel Margin="0,5,5,5" DockPanel.Dock="Top">
<StackPanel Orientation="Horizontal">
<Label>Von</Label>
<DatePicker SelectedDateChanged="DatePickers_SelectedDateChanged" x:Name="DateStartPicker" Width="100"/>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Label>Bis</Label>
<DatePicker SelectedDateChanged="DatePickers_SelectedDateChanged" x:Name="DateEndPicker" Width="100"/>
</StackPanel>
</DockPanel>
<ListView x:Name="ListView" ItemContainerStyle="{DynamicResource ListViewItem}" SelectionChanged="ListView_SelectionChanged" SelectionMode="Single" HorizontalContentAlignment="Stretch" DockPanel.Dock="Bottom" VerticalContentAlignment="Stretch">
<ListView.Resources>
<Style x:Key="ListViewItem" TargetType="{x:Type ListViewItem}">
<EventSetter Event="MouseDoubleClick" Handler="ListViewItem_MouseDoubleClick" />
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.Resources>
<ListView.View>
<GridView x:Name="GridView"/>
</ListView.View>
</ListView>
</DockPanel>
</Grid>
</UserControl>
两张结果截图:
看起来怎么样
它在 XAML-Live-Preview 中的样子
DockPanel 是多余的,因为布局可以由顶级网格应用:
<Grid Loaded="Grid_Loaded">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Grid.Column="0" Margin="5" Orientation="Horizontal">
<Label>Von</Label>
<DatePicker .../>
</StackPanel>
<StackPanel Grid.Column="1" Margin="5" Orientation="Horizontal"
HorizontalAlignment="Right">
<Label>Bis</Label>
<DatePicker .../>
</StackPanel>
<ListView x:Name="ListView" Grid.Row="1" Grid.ColumnSpan="2" ...>
...
</ListView>
</Grid>