Asnivor
Asnivor

Reputation: 266

Issue with WPF WrapPanel inside a ScrollViewer

There are a number of similar questions on SO but so far I have not been able to resolve my problem using them.

I have a bunch of Controls inside a WrapPanel and the WrapPanel is inside a ScrollViewer. The ScrollViewer is inside a Grid.

I am trying to get all the <Border> controls in the WrapPanel to have an Orientation of 'Vertical' (so that they flow down and when there is no more space left vertically they wrap horizontally) with a HorizontalScrollBar that appears when there is no more space left Horizontally.

My code so far is as follows:

<Grid x:Name="configGrid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />                            
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <ScrollViewer Grid.Row="0" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Auto" Width="{Binding ElementName=configGrid, Path=ActualWidth}" Height="{Binding ElementName=configGrid, Path=ActualHeight}">
        <WrapPanel HorizontalAlignment="Left" Orientation="Vertical" x:Name="ConfigWrapPanel" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ScrollViewer}}, Path=ActualWidth}">

            <Border BorderBrush="White" BorderThickness="1,1,1,1" CornerRadius="2" Margin="10">
                <Expander IsExpanded="True" BorderThickness="0" Header="General">
                    // some controls here
                </Expander>
            </Border>

            <Border BorderBrush="White" BorderThickness="1,1,1,1" CornerRadius="2" Margin="10">
                <Expander IsExpanded="True" BorderThickness="0" Header="Another Block">
                    // some controls here
                </Expander>
            </Border>

            // many more <border> blocks here....

        </WrapPanel>
    </ScrollViewer>
</Grid>

This almost works as expected, the various content flows vertically and when there is not enough room at the bottom it moves up and right and starts at the top again. But I never get any horizontal scrollbars and the controls just disappear off the right of the screen.

I'm sure this is something really simple I'm missing but I can't quite figure it out.

As a bit of further info, the various Border controls and sub elements are all of dynamic width and height (which is why I opted for a vertical orientation WrapPanel rather than Horizontal)

Any help would be greatly appreciated.

Upvotes: 1

Views: 1500

Answers (1)

lokusking
lokusking

Reputation: 7456

You have to remove the Width from your WrapPanel.

That width wants to stretch to infinity, which prevents the ScrollViewer from corrent measuring the boundaries of the WrapPanel resulting in never showing the ScrollBar.

Code below shows a working example:

<Grid x:Name="configGrid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <ScrollViewer Grid.Row="0" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Auto"  Height="{Binding ElementName=configGrid, Path=ActualHeight}">
            <WrapPanel HorizontalAlignment="Left" Orientation="Vertical" x:Name="ConfigWrapPanel" >

                <Border BorderBrush="White" BorderThickness="1,1,1,1" CornerRadius="2" Margin="10">
                    <Expander IsExpanded="True" BorderThickness="0" Header="General">
                        // some controls here
                    </Expander>
                </Border>

                <Border BorderBrush="White" BorderThickness="1,1,1,1" CornerRadius="2" Margin="10">
                    <Expander IsExpanded="True" BorderThickness="0" Header="Another Block">
                        // some controls here
                    </Expander>
                </Border>


            </WrapPanel>
        </ScrollViewer>
    </Grid>

Upvotes: 2

Related Questions