2014-09-04 36 views
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; 
} 

如果有人能指出我正確的方向,我將非常高興。 在此先感謝。

回答

0

典型的解決方案可能是在分組函數中設置索引屬性值,或者在分組項目或生成集合視圖時使用的任何方法。