2014-09-28 82 views
0

好吧,我觉得这有点笨,但是,我有一个模板类MyClass或任何其他类型的列表视图,每当我“myListView.Add(new MyClass())”winrt平台添加一个新的UIElement并绑定适当现在,我希望能够遍历这些逻辑项目(myListView.Items或myListView.SelectedItems)并获得它们相应的动画UIElement,这可能吗?如何在列表视图中获取UIElement模板数据?

例如像

class PhoneBookEntry { 
    public String Name { get;set } 
    public String Phone { get;set } 
    public PhoneBookEntry(String name, String phone) { 
     Name = name; Phone = phone; 
    } 
}; 

myListView.Add(new PhoneBookEntry("Schwarzeneger", "123412341234"); 
myListView.Add(new PhoneBookEntry("Stallone", "432143214321"); 
myListView.Add(new PhoneBookEntry("Statham", "567856785678"); 
myListView.Add(new PhoneBookEntry("Norris", "666666666666"); 

而在XAML(只是一个例子,所以我可以解释一下我的意思)

<ListView.ItemTemplate> 
    <DataTemplate> 
     <Grid> 
       <TextBlock Text="{Binding Name}"/> 
       <TextBlock Text="{Binding Phone}"/> 
     </Grid> 
    </DataTemplate> 
</ListView.ItemTemplate> 

所以,我的观点和客观这里是

foreach(PhoneBookEntry pbe in myListView.Items) // or SelectedItems 
{ 
    UIElement el; // How can I get the UIElement associated to this PhoneBookEntry pbe? 
    if(el.Projection == null) 
     el.Projection = new PlaneProjection; 
    PlaneProjection pp = el.Projection as PlaneProjection; 
    // Animation code goes here. 
    if(myListView.SelectedItems.Contains(pbe) 
     //something for selected 
    else 
     //something for not selected 
} 

我只需要一种方法来获得一个UIElement,它被用来表示这个逻辑数据类PhoneBookEntry在te模拟列表视图。 此外,这种必然性伴随着一个非常大的问题,我在哪里,选定的项目在Windows Phone上没有视觉上的差异-_-任何想法?

回答

1

您还可以使用ListView.ContainerFromItem或ListView.ContainerFromIndex方法将在列表视图返回容器UI元素对于一个给定的项目(当然,只有在产生的容器)

+0

我已经用此解决方案进行了测试和研究,它直接回应了基本问题ty。 – Felype 2014-10-02 12:01:06

0

好吧我可能看起来像一个傻瓜回答我自己的问题,但我已经找到了一条出路。首先要做的事情是:ListViews只为列表中的确定项目创建UIElements(缓存的和正在显示的项目)。因此,如果您添加2000个项目到myListView.Items,代表这些项目的UIElements的有效数量将是56或近似数字。 因为,ItemListView模拟UI元素,即使他们不在身边,只是给大小和位置,以滚动条(所以为什么向下滚动非常大名单引起一定的滞后性,WinRT的正在卸载UI元素和加载新的)

从这一点,我想通了,我可以简单地通过

// For each of the cached elements 
foreach(LIstViewItem lvi in myListView.ItemsPanelRoot.Children) 
{ 
    // Inside here I can get the base object used to fill the data template using: 
    PhoneBookEntry pbe = lvi.Content as PhoneBookEntry; 
    if(pbe.Name == "Norris") 
     BeAfraid(); 
    // Or check if this ListViewItem is or not selected: 
    bool isLviSelected = lvi.IsSelected; 
    // Or, like I wanted to, get an UIElement to animate projection 
    UIElement el = lvi as UIElement; 
    if(el.Projection == null) 
     el.Projection = new PlaneProjection(); 
    PlaneProjection pp = el.Projection as PlaneProjection; 
    // Now I can use pp to rotate, move and whatever with this UIElement. 
} 

通过加载UI元素的当前目录遍历所以,这是它。我的鼻子正下方...

+0

ListView类不具有ItemsPanelRoot属性: -/ – bgh 2016-01-04 10:28:34

+0

然后在那里出现了一些非常错误的问题,这个问题将WinRT 8.1视为并使用本文档中指定的对象类型Windows.UI.Xaml.Controls.ListView: https: //msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.listview – Felype 2016-01-04 11:13:01

+0

啊,我明白了。谢谢! – bgh 2016-01-04 11:49:20

相关问题