17

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

1 Answer 1

21

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>
Sign up to request clarification or add additional context in comments.

8 Comments

Sorry, this still doesn't work for me, the items still drop off the bottom of the page, and the scroll bar doesn't become visible. Even specifically setting ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" on the ListView.
@Lee - Then, the problem is with the markup that contains this UserControl. Could add it to your question so I could take a look?
here is the host window <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <ItemsControl x:Name="itemcontrol" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /> </Grid> </Window>
And the code behind that adds the control ` public MainWindow() { InitializeComponent(); Presenters.FolderViewPresenter folderpresenter = new Presenters.FolderViewPresenter(); itemcontrol.Items.Add(folderpresenter.m_View); }`
@Lee - Just use ContentPresenter and set its Content property to an instance of your UserControl: contentPresenter.Content = folderpresenter.m_View; Then you don't need to wrap it in a ScrollViewer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.