1. 程式人生 > >#400 – 使用ItemsPanel 屬性將WrapPanel 作為ListBox的顯示面板(Using a WrapPanel as the Items Panel for a ListBox)

#400 – 使用ItemsPanel 屬性將WrapPanel 作為ListBox的顯示面板(Using a WrapPanel as the Items Panel for a ListBox)

wrappanel 窗口 phoenix orien tails 重寫 lba 常見 lib

原文:#400 – 使用ItemsPanel 屬性將WrapPanel 作為ListBox的顯示面板(Using a WrapPanel as the Items Panel for a ListBox)

ListBoxItemsPanel 屬性可以指定定義顯示各子項的面板的模板。你可以通過定義自己的模板重寫ListBox 常見的垂直堆疊式布局。

如果你設置了ItemsPanel 模板為WrapPanelListBox 將會有WrapPanel的特性。

在下面的例子中,我們將上一篇中的ListBox

稍作修改。指定ItemsPanel 包含一個WrapPanel

<Grid>
        <ListBox ItemsSource="{Binding MovieList}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <Image Source="{Binding Image}" Stretch="None"/>
                        <Label Content="{Binding TitleWithYear}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel IsItemsHost="True" Orientation="Horizontal"  />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>
    </Grid>

可以看到,將ItemsPanel 設置為WrapPanel後,改變窗口的大小,ListBox顯示會自動根據窗口大小換行。

技術分享圖片

技術分享圖片

原文地址:https://wpf.2000things.com/2011/10/04/400-using-a-wrappanel-as-the-items-panel-for-a-listbox/

??

#400 – 使用ItemsPanel 屬性將WrapPanel 作為ListBox的顯示面板(Using a WrapPanel as the Items Panel for a ListBox)