具有多列的 WPF ItemsControl

3
我有一个ScrollViewer,里面包含一个ItemsControl。 ItemsControl的ItemSource绑定到ObservableCollection。
问题在于默认所有内容都位于一列中。 我希望根据窗口的大小,子项可以适应所有可用区域。
WrapPanel会起作用。 请参见下面的图像。 在左侧,项目被排列在ItemsPanel内,在右侧,它们被排列在WrapPanel内。

enter image description here

不幸的是,WrapPanel没有ItemSource属性,因此我无法绑定我的项。有没有办法使ItemsSource具有更多的列或将我的ObservableCollection绑定到WrapPanel?

2个回答

6

只需要反过来做:保留ItemsControl,然后更改其面板模板,使用WrapPanel进行项布局:

<ItemsControl ItemsSource="{Binding YourObservableCollection}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            ...
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

最好为WrapPanel设置ItemWidth - Sadeq Shajary

0
给Wrap Panel设置宽度而不指定方向,就会自动每行显示2个或3个等数量的项目(可以通过调整每个项目的宽度来实现)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接