简体   繁体   中英

ListBox Vertical Scrollbar does not show up (WPF)

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?

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

结果

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM