【问题标题】:AlternationCount not found in listview xaml + application métro在 listview xaml + application métro 中找不到 AlternationCount
【发布时间】:2012-10-09 11:38:00
【问题描述】:

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

最好的问候

【问题讨论】:

  • 它不是 ItemsControl 的一部分,因为它适用于 WPF/Silverlight

标签: xaml listview windows-8 microsoft-metro


【解决方案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 调用)。

【讨论】:

  • 这里有一个例子会有所帮助。
  • @cederlof 你去吧
  • 进一步思考,将索引查找放在IValueConverter 中可能更有意义。
猜你喜欢
  • 1970-01-01
  • 2019-03-04
  • 2018-06-10
  • 1970-01-01
  • 2016-01-21
  • 2011-11-06
  • 1970-01-01
  • 2018-01-29
  • 1970-01-01
相关资源
最近更新 更多