0

我有一个 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>
4

1 回答 1

1

将 ListBox 放在 DockPanel 中,停靠面板的大小应受到限制。

阅读以下 msdn 主题Panels Overview

于 2012-11-20T14:21:24.003 回答