【问题标题】:WPF: ListBox with WrapPanel, vertical scrolling problemWPF:带有 WrapPanel 的 ListBox,垂直滚动问题
【发布时间】:2009-05-04 05:33:18
【问题描述】:

我有一个用户控件(下面的 XAML),它有一个列表框,我想在 WrapPanel 内显示图像,其中显示的图像数量与一行中的图像一样多,然后换行到下一行等。它可以工作,除了当 ListBox 增长到高于窗口中的可用空间时,我没有得到垂直滚动条,即内容被剪裁。如果我在 ListBox 上设置了固定高度,滚动条就会出现并按预期工作。我怎样才能让这个列表框增长到可用空间,然后显示一个垂直滚动条?此控件位于主窗口的 Grid 内的 StackPanel 内。如果我将 StackPanel 包装在 ScrollViewer 中,我会得到我想要的滚动条,但如果我想在 ListBox 上方的 UserControl 中添加更多控件(例如图像大小“缩放”等),这并不是一个好的解决方案,因为我不希望他们与图像一起滚动。

谢谢!! :)

<UserControl x:Class="GalleryAdmin.UI.GalleryView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ListBox Name="itemListBox" BorderThickness="0" ItemsSource="{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Background="LightGray" Margin="5" >
                    <StackPanel Margin="5">
                        <Image Source="{Binding Path=LocalThumbPath}" Height="100" />
                        <TextBlock Text="{Binding Path=Name}" TextAlignment="Center"></TextBlock>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>

【问题讨论】:

    标签: wpf listbox scrollbar wrappanel


    【解决方案1】:

    我认为你最好覆盖 ItemPanelTemplate:

    <Grid>
    <ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel IsItemsHost="True" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBoxItem>listbox item 1</ListBoxItem>
        <ListBoxItem>listbox item 2</ListBoxItem>
        <ListBoxItem>listbox item 3</ListBoxItem>
        <ListBoxItem>listbox item 4</ListBoxItem>
        <ListBoxItem>listbox item 5</ListBoxItem>
    </ListBox>
    

    【讨论】:

    • 禁用水平滚动对我有用!谢谢!
    【解决方案2】:

    好吧,我终于偶然发现了解决方案。我正在将我的 UserControl 添加到如下所示的占位符面板:

                <ScrollViewer Margin="20" >
                    <StackPanel Name="contentPanel"></StackPanel>
                </ScrollViewer>
    

    但是,当我将其切换到 Grid 时,事情开始按我想要的方式进行:

    <Grid Name="contentPanel" Margin="20" />
    

    我认为这与 StackPanel 默认不占用所有垂直空间有关,就像 Grid 正在做的那样。

    【讨论】:

    • 我的ListBox(带有WrapPanel)在StackPanel 内部Grid 内部,并且看到了相同的行为。删除StackPanel 层解决了这个问题。谢谢。
    • 我有两个 Grid 和一个嵌套在 UserControl &lt;UserControl&gt; &lt;Grid&gt; &lt;Grid&gt; &lt;ListBox&gt; 中的 List 删除重复的 Grid 解决了这个问题。
    【解决方案3】:

    我所要做的就是设置以下内容,问题就消失了:

    <ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    

    【讨论】:

      【解决方案4】:

      我只是在查看有关此问题的几个问题,虽然这是一个旧线程,但这个问题给了我答案,但只是为了澄清......

      布局网格是大多数此类问题的答案。为了获得正确的 ListBox/WrapPanel 操作来填充可用空间,下面的代码可以解决问题:

                          <Grid Grid.Row="1" MaxHeight="105">
                              <ListBox ItemTemplate="{DynamicResource StoreGroupTemplate01}" ItemsSource="{Binding StoreGroupHeader}"
                                  ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                                  <ListBox.ItemsPanel>
                                  <ItemsPanelTemplate>
                                          <WrapPanel Orientation="Horizontal"/>
                                  </ItemsPanelTemplate>
                                  </ListBox.ItemsPanel>
                              </ListBox>
                          </Grid>
      

      我将它放在另一个网格中以将列表放置在屏幕底部(即 Grid.Row="1"),您可以调整 MaxHeight(或将其删除)以控制垂直之前的可见区域滚动条会出现。

      【讨论】:

        【解决方案5】:

        将列表框放在 ScrollViewer 中,然后将 scrollviewer 的 VerticalScrollBarVisibility 属性设置为“Auto”

                <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
            <ListBox Name="itemListBox" BorderThickness="0" ItemsSource="{Binding}" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Background="LightGray" Margin="5" >
                        <StackPanel Margin="5">
                            <Image Source="{Binding Path=LocalThumbPath}" Height="100" />
                            <TextBlock Text="{Binding Path=Name}" TextAlignment="Center"></TextBlock>
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>
        </ScrollViewer>
        


        高温

        【讨论】:

        • 谢谢,但不幸的是,这不起作用。它根本没有区别。我实际上已经尝试过了。可能应该提到这一点。这就像 UserControl 不知道它有多少可用的大小??
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-01
        • 2020-06-23
        • 1970-01-01
        • 1970-01-01
        • 2012-04-19
        相关资源
        最近更新 更多