【问题标题】:Windows 8.1 - How to get the index of elements when binding a CollectionViewSourceWindows 8.1 - 绑定 CollectionViewSource 时如何获取元素的索引
【发布时间】:2014-09-04 18:52:48
【问题描述】:

我有一个 Windows 8.1 应用程序。

我的 CollectionViewSource 是按项目创建日期分组的项目列表。现在我已将此 CollectionViewSource 绑定到 ListView 以便显示每个组的组标题,然后显示相应的值。

假设我有 3 个组,如下所示

September 1
    Item-1
    Item-2
    Item-3
September 2
    Item-4
    Item-5
September 3
    Item 6

现在我想在每个组中显示具有备用背景的备用项目。 如果第 1 项为黑色,则第 2 项为白色,第 3 项为黑色。由于第 4 项在第 2 组中,它又是黑色的,依此类推。如果我得到每个组中每个元素的索引,我可以使用转换器来做这个交替背景。如何获取索引?

这是我的 ListViewItemTemplate 的 xaml

<DataTemplate x:Key="MyListViewItemTemplate">
    <Grid Background="{Binding Converter={StaticResource alternateListItemBackgroundConverter}}">
    </Grid>
</DataTemplate>

我应该在上面的 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;
}

如果有人能指出我正确的方向,我会很高兴。 提前致谢。

【问题讨论】:

    标签: c# winrt-xaml windows-8.1 collectionviewsource


    【解决方案1】:

    典型的解决方案可能是在分组函数中设置索引属性值,或者使用它来对项目进行分组或生成集合视图。

    【讨论】:

      猜你喜欢
      • 2016-11-04
      • 2012-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多