WPF ListView滚动条

时间:2011-02-24 17:29:44

标签: wpf

好的,我放弃了 - 如何在列表视图中显示垂直滚动条而不指定xaml中MaxHeight的硬编码值?

这是我的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>

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

也许我问的是错误的问题,应该是:当窗口的高度发生变化时,如何更改listview的最大高度?

请帮助,这已经让我在最后一天开车了......

由于

1 个答案:

答案 0 :(得分:18)

问题来自于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>