WPF 列表视图滚动条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5108235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:37:18  来源:igfitidea点击:

WPF ListView Scrollbars

wpf

提问by Lee

Ok, I give up - how do I get the vertical scroll bars to appear on a list view without specifing a hard coded value for the MaxHeight in the xaml?

好的,我放弃了 - 如何在不为 xaml 中的 MaxHeight 指定硬编码值的情况下让垂直滚动条出现在列表视图中?

here is my xaml (i've not included the data model, but it's basically a directory listing)

这是我的 xaml(我没有包含数据模型,但它基本上是一个目录列表)

<UserControl x:Class="WpfApplication1.Views.FolderViewView"
         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="300" d:DesignWidth="500" >
<DockPanel>
    <StackPanel DockPanel.Dock="Top">
        <Label Name="lblFolder" Content="{Binding Path=FolderName}" MinWidth="250"/>
        <Button Name="btnFolder" Content="Select Folder" Click="btnFolder_Click" />
    </StackPanel>
    <DockPanel>
        <ListView Name="lstFiles" ItemsSource="{Binding}" Margin="1" MaxHeight="200" Height="Auto" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" >
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Filename" DisplayMemberBinding="{Binding Path=FileName}" />
                    <GridViewColumn Header="Extenstion" DisplayMemberBinding="{Binding Path=Extension}" />
                    <GridViewColumn Header="Size" DisplayMemberBinding="{Binding Path=FileSize}" />
                    <GridViewColumn Header="Creation Date" DisplayMemberBinding="{Binding Path=CreateDate}" />
                    <GridViewColumn Header="Modified Date" DisplayMemberBinding="{Binding Path=ModifiedDate}" />
                </GridView>
            </ListView.View>
        </ListView>
    </DockPanel>
</DockPanel>

Without setting MaxHeight on the ListView control, the scroll bar does not appear when there are enough items to make the List view larger than the screen. With MaxHeigt="250", the scroll bar appears, but now the list view doesn't extend when the user changes the size of the window.

如果没有在 ListView 控件上设置 MaxHeight,当有足够的项目使 List 视图大于屏幕时,滚动条不会出现。使用 MaxHeigt="250" 时,会出现滚动条,但现在当用户更改窗口大小时,列表视图不会扩展。

Maybe I'm asking the wrong question and it should be: How do I change the max height of the listview when the height of the window is changed?

也许我问错了问题,应该是:当窗口高度改变时,如何改变列表视图的最大高度?

Please help, this has been driving me up the wall for the last day now...

请帮忙,这已经让我在最后一天爬上墙了......

Thanks

谢谢

Lee

回答by Pavlo Glazkov

The problem comes from the usage of DockPanel. By default it gives its child all the space it requires (regardless available space).

问题来自DockPanel. 默认情况下,它为其子项提供所需的所有空间(无论可用空间如何)。

In order to fix it I suggest you use the Gridpanel instead of DockPanel:

为了修复它,我建议您使用Grid面板而不是DockPanel

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <StackPanel Grid.Row="0">
        <Label Name="lblFolder" Content="{Binding Path=FolderName}" MinWidth="250"/>
        <Button Name="btnFolder" Content="Select Folder" Click="btnFolder_Click" />
    </StackPanel>


    <ListView Grid.Row="1" Name="lstFiles" ItemsSource="{Binding}" Margin="1" >
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Filename" DisplayMemberBinding="{Binding Path=FileName}" />
                <GridViewColumn Header="Extenstion" DisplayMemberBinding="{Binding Path=Extension}" />
                <GridViewColumn Header="Size" DisplayMemberBinding="{Binding Path=FileSize}" />
                <GridViewColumn Header="Creation Date" DisplayMemberBinding="{Binding Path=CreateDate}" />
                <GridViewColumn Header="Modified Date" DisplayMemberBinding="{Binding Path=ModifiedDate}" />
            </GridView>
        </ListView.View>
    </ListView>
</Grid>