簡體   English   中英

ListBox垂直滾動條不顯示(WPF)

[英]ListBox Vertical Scrollbar does not show up (WPF)

我有一個視圖,該視圖應顯示信息並進行測試,我添加了testdata。 但是,如果我為屏幕空間添加了太多項目,則不會出現垂直滾動條。

這是我的XML代碼:

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

我嘗試添加滾動查看器,但無法正常工作。 我是在做錯事還是我可能出什么問題?

只需在您的xaml代碼中刪除StackPanel 您不需要它,只會遇到問題。 我建議您更改樣式布局,因為您不應該使用Grid

當您想在頂部顯示Label並在Label下顯示List時,至少添加一些RowDefinitions來布局UI。

看起來像這樣

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

結果

只需在您的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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM