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

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

问题描述

我有一个UserControl(下面的XAML),它具有一个ListBox,我想在WrapPanel中显示图像,在该图像中显示的图像将尽可能多地显示在一行上,然后包装到下一行,等等.它可以正常工作,除了当ListBox变得大于窗口中的可用空间时,我没有得到垂直的滚动条,即内容被剪切了.如果我在ListBox上设置了固定的高度,则会出现滚动条并按预期工作.如何使此列表框增长到可用空间,然后显示垂直滚动条?该控件位于主窗口中网格内部的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>

但是,当我改用Grid时,事情开始按照我想要的方式工作:

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天全站免登陆