Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ListView Scrollbars

Tags:

wpf

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?

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

<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.

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

like image 325
Lee Avatar asked Feb 24 '11 17:02

Lee


1 Answers

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

In order to fix it I suggest you use the Grid panel instead of 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>
like image 63
Pavlo Glazkov Avatar answered Oct 09 '22 13:10

Pavlo Glazkov