1

为什么我在 Windows8 应用程序的列表视图 XAML 中找不到成员 AlternationCount?

此致

4

1 回答 1

1

为此,您可以使用ItemContainerGenerator属性并将其ContainerFromItemIndexFromContainer方法链接起来以获取项目的索引,然后使用转换器从索引中获取背景颜色。

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)。

于 2012-10-10T00:47:23.280 回答