【问题标题】:how to display vertical scrollbar in ListBox with WrapPanel如何使用 WrapPanel 在 ListBox 中显示垂直滚动条
【发布时间】:2012-11-20 14:12:00
【问题描述】:

我有一个 ListBox,它在 WrapPanel 中显示其项目。 ListBox 本身嵌入在 Border 中。但是我放入 ListBox 的项目越多,窗口就越大。如何防止这种情况并改为显示垂直滚动条?

我找到了其他几个帖子,但到目前为止没有任何效果。

            <Border Background="WhiteSmoke" BorderBrush="Gray" BorderThickness="4" Margin="5"> 
            <ListBox Background="Transparent" BorderThickness="0" Height="Auto" 
                     ItemsSource="{Binding Path=Snapshots, RelativeSource={RelativeSource AncestorType=Demo:CameraCanvas}}"
                     SelectedItem="{Binding Path=Snapshots.SelectedSnapshot, RelativeSource={RelativeSource AncestorType=Demo:CameraCanvas}}"
                     ScrollViewer.VerticalScrollBarVisibility="Auto"
                     ScrollViewer.CanContentScroll="True"
                     >
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel 
                            Width="{Binding (FrameworkElement.ActualWidth), 
                            RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}" 
                            ItemWidth="{Binding (ListView.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListView}}"
                            MinWidth="{Binding ItemWidth, RelativeSource={RelativeSource Self}}" 
                            ItemHeight="{Binding (ListView.View).ItemHeight, RelativeSource={RelativeSource AncestorType=ListView}}" />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Border CornerRadius="5" Background="WhiteSmoke" BorderBrush="Gray" BorderThickness="2" Margin="5" Padding="2">
                            <StackPanel Orientation="Vertical">
                                <Image Source="{Binding Path=Image, UpdateSourceTrigger=PropertyChanged}" Width="64" Height="64" Stretch="Uniform" StretchDirection="Both" />
                            </StackPanel>
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            </Border>

【问题讨论】:

    标签: c# listbox scrollbar wrappanel


    【解决方案1】:

    将ListBox放在DockPanel中,dockpanel的大小应该有限制。

    阅读以下msdn主题Panels Overview

    【讨论】:

    • 但是 DockPanel (ListBox) 从上到下显示在我的窗口右侧。当我调整窗口大小时,列表框当然应该变大。但你是说,我应该设置一个大小限制?
    • @Matthias 尝试设置大小,您会看到滚动查看器会出现。停靠面板在 X 和 Y 维度上都有限制,因此它将从其视觉父级接收其大小,并且不会允许列表框超过此大小。
    • 没用。我的第一个控件是一个网格,在右边的单元格上,我确实有带有内部 ListBox/WrapPanel 的边框。如果我在边框周围放置一个 DockPanel 说 LastChildFill=true 它仍然不起作用。我会在另一个原型中尝试一下,然后告诉你。
    • @Matthias 将网格单元格的行大小设置为“*”而不是“自动”
    • 谢谢,现在可以了。我的一个主要问题是,主窗口设置了一个属性“SizeToContent”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多