【发布时间】:2012-10-09 11:38:00
【问题描述】:
为什么在我的 Windows8 应用程序的列表视图 XAML 中找不到成员 AlternationCount??
最好的问候
【问题讨论】:
-
它不是 ItemsControl 的一部分,因为它适用于 WPF/Silverlight
标签: xaml listview windows-8 microsoft-metro
为什么在我的 Windows8 应用程序的列表视图 XAML 中找不到成员 AlternationCount??
最好的问候
【问题讨论】:
标签: xaml listview windows-8 microsoft-metro
为此,您可以使用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 调用)。
【讨论】:
IValueConverter 中可能更有意义。