为什么我在 Windows8 应用程序的列表视图 XAML 中找不到成员 AlternationCount?
此致
为此,您可以使用ItemContainerGenerator
属性并将其ContainerFromItem
和IndexFromContainer
方法链接起来以获取项目的索引,然后使用转换器从索引中获取背景颜色。
public class Item : BindableBase
{
public ItemsControl itemsControl {get;set;}
private string name;
public string Name
{
get{
return name;
}
set
{
name = value;
OnPropertyChanged();
}
}
public int Index
{
get
{
return itemsControl.ItemContainerGenerator.IndexFromContainer(
itemsControl.ItemContainerGenerator.ContainerFromItem(this)
);
}
}
}
public sealed partial class ItemContainerGeneratorTest : App1.Common.LayoutAwarePage
{
...
public ObservableCollection<Item> test
{
get
{
var test = new ObservableCollection<Item>();
test.Add(new Item() { Name = "Index for item 1: ", itemsControl = ItemsControlControl });
test.Add(new Item() { Name = "Index for item 2: ", itemsControl = ItemsControlControl });
test.Add(new Item() { Name = "Index for item 3: ", itemsControl = ItemsControlControl });
test.Add(new Item() { Name = "Index for item 4: ", itemsControl = ItemsControlControl });
test.Add(new Item() { Name = "Index for item 5: ", itemsControl = ItemsControlControl });
test.Add(new Item() { Name = "Index for item 6: ", itemsControl = ItemsControlControl });
return test;
}
}
...
}
<ItemsControl x:Name="ItemsControlControl" ItemsSource="{Binding ElementName=pageRoot, Path=test}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Index}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
请注意,此示例当前不处理对集合的更改(即在集合更改时OnPropertyChanged
不调用Index
)。