Search code examples
c#wpfuser-controlsscrollviewer

Create a usercontrol with a ScrollView


I have a WPF project.

In this project I have a UserControl with a StackPanel containing various elements. It's within a cell in a Grid. When i resize the Windwo and the Cell becomes to small to fit the Stackpanel I want the scrollview to take over.

I tried putting TheUserControl in a but that only seems to work with Set size. I need it to adjust to the dynamic cell size. All "Solutions" I found online have been unnecessary difficult workarounds for such a simple and commen problem. So I'm pretty sure that there is an easy way to achive this behaviour.

Pseudo-Code

The UserControl:

<UserControl x:class=x.TheUserControl>
    <StackPanel>
        <Label Content="Label 01 />
        <Label Content="Label 02 />
        <Label Content="Label 03 />
                     .
                     .
                     .
        <Label Content="Label n />
    </StackPanel>
</UserControl>

The Window:

<Window x:Class="x.MainWindow>
<Grid>

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

    <Label Content="Header" />

    <ScrollView Grid.Row="1">
        <x:TheUserControl />
    </ScrollView>

</Window>

I'm pretty sure ScrollView works just fine when i put a StackPAnel directly into the ScrollView, so why is it so complicated with a UserControl inbetween?

I'm properly unaware of some obvious behavior in ScrollView and I would be really glad if someone could show me a better approach or explain why it beahaves this way.


Solution

  • Please try to use the ScrollViewer inside of your usercontrol and use HorizontalScrollBarVisibility & VerticalScrollBarVisibility:

    userControl:

    <UserControl x:Class="WpfApp1.TheUserControl"
                 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" 
                 xmlns:local="clr-namespace:WpfApp1"
                 >
        <Grid>
            <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                <StackPanel Orientation="Vertical"  Background="LightGray">
                    <Label Content="Label 01" />
                    <Label Content="Label 02" />
                    <Label Content="Label 03" />
                    <Label Content="Label n" />
                </StackPanel>
            </ScrollViewer>
        </Grid>
    </UserControl>
    

    MainWindow:

    <Window x:Class="WpfApp1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1"
            mc:Ignorable="d"
            Title="MainWindow" >
        <Grid Background="Aqua">
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
    
            <Label Content="Header" Grid.Row="0" />
            <local:TheUserControl Grid.Row="1" />
    
        </Grid>
    </Window>
    

    Result, if you resize the window:

    Usercontrol with Scrollviewer inside