WPF:带有 WrapPanel 的 ListBox,垂直滚动问题 [英] WPF: ListBox with WrapPanel, vertical scrolling problem

查看:24
本文介绍了WPF:带有 WrapPanel 的 ListBox,垂直滚动问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UserControl (XAML below) that has a ListBox that I want to display images inside a WrapPanel, where images are displayed as many as will fit on one row and then wrap onto the next row etc. It works, except when the ListBox grows higher than the available space in the window, I'm not getting a vertical scrollbar, i.e. the contents get clipped. If I set a fixed height on the ListBox, the scrollbar appears and works as expected. How can I get this listbox to grow to the available space and then show a vertical scrollbar? This control is inside StackPanel inside a Grid in the main window. If I wrap the StackPanel inside a ScrollViewer, I get the scrollbar I'm after, but that's not really a good solution if I wanted to add some more controls to the UserControl above the ListBox (e.g. image size "zoom" etc) as I wouldn't want them to scroll with the images.

谢谢!!:)

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

推荐答案

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

Well, I finally stumbled upon the solution. I was adding my UserControl to a placeholder panel that looked like this:

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

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

However, when I switched it to a Grid instead, things started to work the way I wanted:

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

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

I think it has to do with the StackPanel not taking up all the vertical space by default, like the Grid is doing.

这篇关于WPF:带有 WrapPanel 的 ListBox,垂直滚动问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆