【问题标题】:ListBox Vertical Scrollbar does not show up (WPF)列表框垂直滚动条不显示(WPF)
【发布时间】:2017-06-22 21:00:06
【问题描述】:

我有一个应该显示信息并对其进行测试的视图,我添加了 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>

我尝试添加一个滚动查看器,但没有正常工作。我是不是做错了什么或者我可能是什么问题?

【问题讨论】:

    标签: c# wpf listbox


    【解决方案1】:

    只需删除 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">
    

    【讨论】:

    • WPF 的新手。感谢您在编辑中的进一步解释。可能应该读一本书而不是只显示代码而没有进一步解释的教程
    • 欢迎您,我们都开始了一次 :) 祝您学习 WPF 好运
    【解决方案2】:

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

    【讨论】:

    • 看看我的回答。只需删除 StackPanel 并使用 Grid 就像您应该使用它一样,您不必添加滚动查看器;)
    • 是的,正如@Mighty Badaboom 所建议的,ScrollViewers 不适用于 StackPanels。这是因为 StackPanel 使用无限空间测量其子级:stackoverflow.com/questions/41140287/…
    猜你喜欢
    • 2023-03-30
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多