WPF Listview溢出

时间:2017-11-16 01:15:12

标签: c# wpf xaml listview

如何阻止listview控件溢出?

添加项目时:

列表视图应该停在红色标记处。

期望的结果: enter image description here

MainWindow.xaml是一个汉堡包菜单,左边是菜单,右边是views.accounts.xaml。

Views.Accounts.xaml:

    <UserControl>

<Grid Background="White">
    <!--Define 2 Rows-->
    <Grid.RowDefinitions>
        <RowDefinition Height="48"/>
        <RowDefinition/>
    </Grid.RowDefinitions>

<!--Header-->
<Border Grid.Row="0" Background="#FF444444"...>
<Grid Grid.Row="1" Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="250"/>
        </Grid.ColumnDefinitions>

<!--Options-->
<StackPanel Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"...>

<!--Content area-->
<Grid Grid.Row="1" Grid.Column="0" Margin="0 20 0 0">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <ScrollViewer Grid.Row="0">
            <ListView Style="{StaticResource VirtualisedMetroListView}"
                      MinHeight="250" Name="accountsListView">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Username" DisplayMemberBinding="{Binding username}"/>
                        <GridViewColumn Header="Password" DisplayMemberBinding="{Binding password}"/>
                        <GridViewColumn Header="Email" DisplayMemberBinding="{Binding email}"/>
                        <GridViewColumn Header="Verified" DisplayMemberBinding="{Binding verified}"/>
                    </GridView>
                </ListView.View>
            </ListView>
                </ScrollViewer>
                <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right">
                <Button Content="Clear" Width="74" Click="clear_Click"/>
                <Button Margin="10 0 0 0" Content="Export" Width="74"/>
            </StackPanel>
            </Grid>
        </Grid>

<!--Settings-->
<Grid Grid.Row="1" Grid.Column="1" Margin="10 10 0 10">

</Grid>
</Grid>
</UserControl>

1 个答案:

答案 0 :(得分:1)

通常对于这种问题,没有什么理由:

  • ListView位于StackPanel
  • ListView位于自动高度网格行内。
  • ListView位于ScrollViewer中。

解决此问题的最简单方法是删除StackPanel和ScrollViewer控件。 ListView包含内置的ScrollViewer,因此您无需手动定义它。

还要确保Grid的RowDefinition未设置为Height =“Auto”。

例如:

<!--Content area-->
<Grid Grid.Row="1" Grid.Column="0" Margin="0 20 0 0">
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <ListView Style="{StaticResource VirtualisedMetroListView}" MinHeight="250" Name="accountsListView">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Username" DisplayMemberBinding="{Binding username}"/>
                <GridViewColumn Header="Password" DisplayMemberBinding="{Binding password}"/>
                <GridViewColumn Header="Email" DisplayMemberBinding="{Binding email}"/>
                <GridViewColumn Header="Verified" DisplayMemberBinding="{Binding verified}"/>
            </GridView>
        </ListView.View>
    </ListView>
    <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right">
        <Button Content="Clear" Width="74" Click="clear_Click"/>
        <Button Margin="10 0 0 0" Content="Export" Width="74"/>
    </StackPanel>
</Grid>

还要确保VirtualisedMetroListView-style不包含任何可能影响ScrollViewer的内容。