简体   繁体   English

如何从ListView中的模板数据中获取UIElement?

[英]How to get UIElement out of templated data in ListView?

Okay, i feel slightly dumb for askin this but, I have a listview with a templated class MyClass or whatever, whenever i "myListView.Add(new MyClass())" the winrt platform adds a new UIElement there and binds the proper properties into their proper uielements properly, now, I want to be able to iterate through these logical items (myListView.Items or myListView.SelectedItems) and get their corresponding UIElement for animation, is that possible? 好的,我对此感到有点傻,但是,每当我在myrtView平台上“ myListView.Add(new MyClass())”在那里添加新的UIElement并将适当的属性绑定到其中时,我就有一个带有模板类MyClass或类似内容的listview现在,我希望能够正确遍历这些逻辑项(myListView.Items或myListView.SelectedItems)并获取其对应的UIElement动画,这可能吗?

like for example 例如

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");

And in XAML (just an example so i can explain what I mean) 在XAML中(只是一个例子,我可以解释一下我的意思)

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

So, my point and objective here is to 所以,我的目的和目标是

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
}

I just need a way to get an UIElement which is being used to represent this logical data class PhoneBookEntry in the templated listview. 我只需要一种获取UIElement的方法,该UIElement用来在模板化列表视图中表示此逻辑数据类PhoneBookEntry。 Also, this necessity comes with a very big problem I'm having where, selected items doesn't differ visually on Windows Phone -_- any ideas? 另外,这种必要性带来了一个很大的问题,我在哪里遇到问题,所选项目在Windows Phone上在视觉上没有区别-_-有什么想法吗?

Ok I may look like a fool answering my own question but i've figured a way out. 好的,我看起来像个傻瓜在回答我自己的问题,但我已经找到了出路。

First things first: ListViews only create UIElements for determinate items in the list (the ones cached and the ones being shown). 首先,第一件事:ListViews仅创建UIElements来确定列表中的项目(缓存的项目和显示的项目)。 So if you do add 2000 items to myListView.Items, the effective ammount of UIElements representing these items will be 56 or close number. 因此,如果确实将2000个项目添加到myListView.Items,则表示这些项目的UIElement的有效数量将为56或结束编号。 Because, the ItemListView simulates the UIElements even if they're not there, just to give size and position to the scrollbar (hence why scrolling down on very large lists cause some lag, WinRT is unloading UIElements and loading new ones) 因为,ItemListView会模拟UIElement,即使它们不在那里也只是为了给滚动条提供大小和位置(因此,为什么在非常大的列表上向下滚动会导致一些滞后,WinRT会卸载UIElement并加载新的UIElement)。

From that, I figured out I could simply iterate through the current list of loaded UIElements through 由此,我发现我可以简单地遍历当前加载的UIElement列表,通过

// 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.
}

So, this is it. 就是这样。 Right beneath my nose... 就在我的鼻子下面...

您还可以使用ListView.ContainerFromItem或ListView.ContainerFromIndex方法,这将返回列表视图中给定项目的容器UI元素(当然,仅当生成容器时)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM