0

I have a view that should display information and to test it I added testdata. However if I add too much items for screen space the vertical scroll bar does not appear.

This is my XML Code:

<UserControl x:Class="not relevant"
    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="not relevant"
    mc:Ignorable="d"
     Width="250">

<Border Background="{DynamicResource  BG}" BorderBrush="{DynamicResource  BorderBrush}" BorderThickness="1">
    <StackPanel>
        <Grid>
            <Label  Height="30" Content="Geöffnete Designs" HorizontalContentAlignment="Center"></Label>
            <Separator Background="DarkGray"/>
            <ListBox  Background="{DynamicResource BG}" BorderThickness="0" VerticalAlignment="Stretch" >
                <ListBoxItem>
                    <Label Content="Hallo"></Label>
                </ListBoxItem>
                <ListBoxItem>
                    <Label Content="Hallo"></Label>
                </ListBoxItem>
                <ListBoxItem>
                    <Label Content="Hallo"></Label>
                </ListBoxItem>
                <ListBoxItem>
                    <Label Content="Hallo"></Label>
                </ListBoxItem>
                <ListBoxItem>
                    <Label Content="Hallo"></Label>
                </ListBoxItem>
                <ListBoxItem>
                    <Label Content="Hallo"></Label>
                </ListBoxItem>
                <ListBoxItem>
                    <Label Content="Hallo"></Label>
                </ListBoxItem>
                <ListBoxItem>
                    <Label Content="Hallo"></Label>
                </ListBoxItem>
                <ListBoxItem>
                    <Label Content="Hallo"></Label>
                </ListBoxItem>
                <ListBoxItem>
                    <Label Content="Hallo"></Label>
                </ListBoxItem>
                <ListBoxItem>
                    <Label Content="Hallo"></Label>
                </ListBoxItem>
            </ListBox>
        </Grid>
    </StackPanel>
</Border>

I tried adding a Scroll viewer instead but that didn't work properly. Am I doing something wrong or what might me the problem?

hullunist
  • 1,117
  • 2
  • 11
  • 31

2 Answers2

2

Just remove the StackPanel in your xaml code. You don't need it and makes only problems. I would suggest changing your styling layout because you don't use a Grid like you should.

At least add some RowDefinitions to layout your UI when you want to show the Label at top and the List under the Label.

Could look like this

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Label Grid.Row="0"
           Height="30"
           Content="Geöffnete Designs"
           HorizontalContentAlignment="Center" />
    <Separator Grid.Row="1"
               Background="DarkGray" />
    <ListBox Grid.Row="2"
             BorderThickness="0"
             VerticalAlignment="Stretch">

Result

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51
  • Pretty new to WPF. Thank you for the further explanation in your edit. Should probably read a book instead of tutorials which only show code without further explanation – hullunist Jun 22 '17 at 13:00
  • Your welcome, we all started once :) Good luck with learning WPF – Mighty Badaboom Jun 22 '17 at 13:01
0

Just add a scroll viewer in your xaml

    <ScrollViewer>
    <Border BorderThickness="1">
        <StackPanel>
            <Grid>
                <Label  Height="30" Content="Geöffnete Designs" HorizontalContentAlignment="Center"></Label>
                <Separator Background="DarkGray"/>
                <ListBox BorderThickness="0" VerticalAlignment="Stretch" >
                    <ListBoxItem>
                        <Label Content="Hallo"></Label>
                    </ListBoxItem>
                    <ListBoxItem>
                        <Label Content="Hallo"></Label>
                    </ListBoxItem>
                    <ListBoxItem>
                        <Label Content="Hallo"></Label>
                    </ListBoxItem>
                    <ListBoxItem>
                        <Label Content="Hallo"></Label>
                    </ListBoxItem>
                    <ListBoxItem>
                        <Label Content="Hallo"></Label>
                    </ListBoxItem>
                    <ListBoxItem>
                        <Label Content="Hallo"></Label>
                    </ListBoxItem>
                    <ListBoxItem>
                        <Label Content="Hallo"></Label>
                    </ListBoxItem>
                    <ListBoxItem>
                        <Label Content="Hallo"></Label>
                    </ListBoxItem>
                    <ListBoxItem>
                        <Label Content="Hallo"></Label>
                    </ListBoxItem>
                    <ListBoxItem>
                        <Label Content="Hallo"></Label>
                    </ListBoxItem>
                    <ListBoxItem>
                        <Label Content="Hallo"></Label>
                    </ListBoxItem>
                </ListBox>
            </Grid>
        </StackPanel>
    </Border>
</ScrollViewer>
Daniele Sartori
  • 1,674
  • 22
  • 38
  • Have a look at my answer. Just remove the `StackPanel` and use the `Grid` like you should use it and you don't have to add a scroll viewer ;) – Mighty Badaboom Jun 22 '17 at 12:56
  • Yes, as suggested by @Mighty Badaboom, ScrollViewers don't work well with StackPanels. This is because a StackPanel measures its children with an infinite space: https://stackoverflow.com/questions/41140287/horizontal-scroll-for-stackpanel-doesnt-work/41140885#41140885 – mm8 Jun 22 '17 at 12:58