如何使用WrapPanel在ListBox中显示垂直滚动条

时间:2012-11-20 14:12:00

标签: c# listbox scrollbar wrappanel

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

我发现了其他一些帖子,但到目前为止没有任何工作。

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>

1 个答案:

答案 0 :(得分:1)

将ListBox放在DockPanel中,Dockpanel的大小应该受到约束。

阅读以下msdn主题Panels Overview