1
我有一个Windows 8.1应用程序。Windows 8.1 - 如何在绑定CollectionViewSource时获取元素的索引
我的CollectionViewSource是按项目创建时按日期分组的项目列表。现在我已将这个CollectionViewSource绑定到一个ListView,以便显示每个组的组标题,然后显示相应的值。
比方说,我有3个组如下
September 1
Item-1
Item-2
Item-3
September 2
Item-4
Item-5
September 3
Item 6
现在我要显示的每个组交替背景的备选项目。 如果Item-1是黑色的,则Item-2是白色的,Item-3是黑色的。由于项目4在组2中,所以它又是黑色的等等。如果我得到每个组中每个元素的索引,我可以使用转换器来完成这种替代背景。我如何获得索引?
这里是我的我的ListViewItemTemplate
<DataTemplate x:Key="MyListViewItemTemplate">
<Grid Background="{Binding Converter={StaticResource alternateListItemBackgroundConverter}}">
</Grid>
</DataTemplate>
我应该在上面的XAML绑定得到,我可以在我的转换器使用,如下图所示索引的XAML。这里是我转换功能的转换器
public object Convert(object value, Type targetType, object parameter, string language)
{
int index = value as int;
if (value == null || !int.TryParse(value.ToString(), out index))
{
throw new ArgumentException("The value passed to this converter must be an integer value", "value");
}
return index % 2 == 0 ? Colors.Black : Colors.White;
}
如果有人能指出我正确的方向,我将非常高兴。 在此先感谢。