gpt4 book ai didi

c# - 当元素数量大于 n 时,如何在 ItemsControl 中显示 "more item"?

转载 作者:行者123 更新时间:2023-11-30 14:55:46 25 4
gpt4 key购买 nike

我有一个 ItemsControl以 2 列 4 行的 Windows 8 方式显示磁贴。每个图 block 都是可点击的,并会触发一个命令,将所选项目加载到另一个 View 中。

我的问题从这里开始:我的边界IList<>一次可以包含超过 8 个元素,但必须显示不超过 8 个图 block 。

我想要实现的是创建一种不同类型的图 block (链接到另一个命令),它只会在我的 Converter 时出现(例如:使用 IList<> )大于 8。请查看下图以了解我的目标。

Awaited result

到目前为止,我可以限制在 IList<> 中检索的元素数量当容器大于 8 时,容器变为 7,但添加“特殊”第 8 元素对我来说仍然是个谜。

最佳答案

我已经使用 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>

请注意,我已经为两种不同的类型定义了数据模板,即 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; }

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

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

您还可以使用 Attached PropertiesConverters 来简化此操作或执行更复杂的功能。

结果

result

关于c# - 当元素数量大于 n 时,如何在 ItemsControl 中显示 "more item"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24625244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com