ItemsControl 可以对绑定的集合进行分组吗?

发布于 2024-12-29 01:14:03 字数 1216 浏览 3 评论 0原文

我试图确定是否有一种方法可以使用 ItemsControl 将 ItemsSource 中的项目分组到单独的 ItemsPanel 中。具体来说,我正在尝试创建一个视图,以便可以以网格类型的方式列出 8 个项目的集合,例如 UniformGrid,但结果均匀,而不是空单元格。

虽然源中包含 8 个项目的 UniformGrid 会产生如下结果:(

-------------------------------------
- +++++++++ - +++++++++ - +++++++++ -
-------------------------------------
- +++++++++ - +++++++++ - +++++++++ -
-------------------------------------
- +++++++++ - +++++++++ - ooooooooo -
-------------------------------------

最后一个单元格为空)

我试图产生如下结果:

-------------------------------------
- +++++++++ - +++++++++ - +++++++++ -
-------------------------------------
- +++++++++ - +++++++++ - +++++++++ -
-------------------------------------
- +++++++++++++++ - +++++++++++++++ -
-------------------------------------

如果我以编程方式将其分解,我可以通过像这样嵌套来轻松获得显示:

<StackPanel Orientation="Horizontal">
   <UniformGrid>
      item 1
      item 2
      item 3
   </UniformGrid>
   <UniformGrid>
      item 4
      item 5
      item 6
   </UniformGrid>
   <UniformGrid>
      item 7
      item 8
   </UniformGrid>
</StackPanel>

但我想通过 Xaml 来达到我想要的结果。

I'm trying to determine if there's a way to use an ItemsControl to group items in the ItemsSource into individual ItemsPanels. Specifically, I'm trying to create a view such that a collection of say, 8 items can be listed in grid-type fashion, like a UniformGrid, but with even results, and not empty cells.

While a UniformGrid with 8 items in the source would produce results like the following:

-------------------------------------
- +++++++++ - +++++++++ - +++++++++ -
-------------------------------------
- +++++++++ - +++++++++ - +++++++++ -
-------------------------------------
- +++++++++ - +++++++++ - ooooooooo -
-------------------------------------

(the last cell being empty)

I'm trying to produce results like so:

-------------------------------------
- +++++++++ - +++++++++ - +++++++++ -
-------------------------------------
- +++++++++ - +++++++++ - +++++++++ -
-------------------------------------
- +++++++++++++++ - +++++++++++++++ -
-------------------------------------

If I programmatically break it up, I can get the display easily by nesting like so:

<StackPanel Orientation="Horizontal">
   <UniformGrid>
      item 1
      item 2
      item 3
   </UniformGrid>
   <UniformGrid>
      item 4
      item 5
      item 6
   </UniformGrid>
   <UniformGrid>
      item 7
      item 8
   </UniformGrid>
</StackPanel>

But I'd like to acheive the results I want just through Xaml.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咽泪装欢 2025-01-05 01:14:03

我找到了一种方法,方法是在绑定到集合的 DataTemplate 中嵌套一个新的 ItemsControl,并使用 ValueConverter 将集合转换为数组的数组。

<ItemsControl ItemsSource="{Binding MyCollection, Converter={StaticResource ArraySplitConverter}, ConverterParameter=3}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid IsItemsHost="True" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Label Content="{Binding Title} />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

和值转换器:

public class ArraySplitConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int param = System.Convert.ToInt16(parameter);
        object[] coll = (object[])value;
        ArrayList outer = new ArrayList();
        ArrayList inner = new ArrayList();

        for (int i = 0; i < coll.Length; i++)
        {
            inner.Add(coll[i]);
            if (((i + 1) % param == 0) || (i == coll.Length - 1)) { outer.Add(inner); inner = new ArrayList(); }
        }

        return outer;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

I found the way to do it by nesting an new ItemsControl in the DataTemplate of the one bound to the collection, and using a ValueConverter to convert the collection into an array of arrays.

<ItemsControl ItemsSource="{Binding MyCollection, Converter={StaticResource ArraySplitConverter}, ConverterParameter=3}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid IsItemsHost="True" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Label Content="{Binding Title} />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

And the ValueConverter:

public class ArraySplitConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int param = System.Convert.ToInt16(parameter);
        object[] coll = (object[])value;
        ArrayList outer = new ArrayList();
        ArrayList inner = new ArrayList();

        for (int i = 0; i < coll.Length; i++)
        {
            inner.Add(coll[i]);
            if (((i + 1) % param == 0) || (i == coll.Length - 1)) { outer.Add(inner); inner = new ArrayList(); }
        }

        return outer;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文