当元素数量大于n时,如何在ItemsControl中显示"更多项目"?

Aym*_*ric 3 c# wpf itemscontrol

我有一个ItemsControl以Windows 8方式显示的图块,包含2列和4行.每个磁贴都是可单击的,并触发一个命令,该命令将在另一个视图中加载所选项.

我的问题从这里开始:我的绑定IList<>当时可以包含8个以上的元素,但必须显示不超过8个tile.

我想要实现的是创建一个不同类型的图块(链接到另一个命令),Converter只有当我IList<>的大于8 时才会出现(例如:使用a ).请检查下面的图纸以了解我的目标.

等待结果

到目前为止IList<>,只要大于8,我就可以将容器中检索到的元素数限制为7,但添加"特殊"第8个元素对我来说仍然是个谜.

pus*_*raj 5

我曾经CompositeCollection解决过这个问题,这使得多个集合和项目可以显示为单个列表.更多关于CompositeCollection

这是一个样本

XAML

<ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="2" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.Resources>
        <DataTemplate DataType="{x:Type sys:Int32}">
            <Border Margin="4"
                    Background="LightSkyBlue">
                <TextBlock Text="{Binding}" FontSize="15"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center" />
            </Border>
        </DataTemplate>
        <DataTemplate DataType="{x:Type sys:String}">
            <Border Margin="4"
                    Background="MediumPurple">
                <TextBlock Text="{Binding}" FontWeight="SemiBold"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center" />
            </Border>
        </DataTemplate>
    </ItemsControl.Resources>      
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

请注意,我已经为两种不同的类型定义了数据模板,即int和string,这将有助于我相应地呈现相同的数据模板

    public ViewModel()
    {
        IEnumerable<int> originalData = Enumerable.Range(1, 12);
        Items = new CompositeCollection();
        Items.Add(new CollectionContainer() { Collection = originalData.Take(originalData.Count() > 8 ? 7 : 8) });
        if (originalData.Count() > 8)
            Items.Add(originalData.Count() - 7 + " more");
    }

    public CompositeCollection Items { get; set; }
Run Code Online (Sandbox Code Playgroud)

整个想法是限制主要集合中的元素数量,并为不同类型的集合添加额外的元素,例如原始列表是int,extra是字符串

因此,项目控件将呈现集合中的所有元素,并且我们可以根据数据类型控制外观

您也可以使用Attached PropertiesConverters简化此功能或执行更复杂的功能.

结果

结果