【问题标题】:How to get UIElement out of templated data in ListView?如何从 ListView 中的模板数据中获取 UIElement?
【发布时间】:2014-09-28 18:05:17
【问题描述】:

好的,我觉得这个问题有点愚蠢,但是,我有一个带有模板类 MyClass 或其他内容的列表视图,每当我“myListView.Add(new MyClass())”时,winrt 平台都会在那里添加一个新的 UIElement 并绑定正确的属性到正确的uielements中,现在,我希望能够遍历这些逻辑项(myListView.Items或myListView.SelectedItems)并获取它们对应的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,该 UIElement 用于在模板化列表视图中表示这个逻辑数据类 PhoneBookEntry。 此外,这种必要性伴随着我遇到的一个非常大的问题,在 Windows Phone 上,所选项目在视觉上没有差异 -_- 有什么想法吗?

【问题讨论】:

    标签: c# xaml windows-runtime windows-phone-8.1


    【解决方案1】:

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

    【讨论】:

    • 我已经测试和研究过这个解决方案,它直接回答了基本问题,ty。
    【解决方案2】:

    好吧,我在回答自己的问题时可能看起来像个傻瓜,但我已经找到了出路。

    首先要做的事情:ListViews 只为列表中的确定项目(缓存的项目和显示的项目)创建 UIElement。因此,如果您确实将 2000 个项目添加到 myListView.Items,则表示这些项目的 UIElements 的有效数量将是 56 或接近的数字。 因为,即使 UIElements 不存在,ItemListView 也会模拟 UIElements,只是为了给滚动条提供大小和位置(因此,为什么在非常大的列表上向下滚动会导致一些延迟,WinRT 正在卸载 UIElements 并加载新的 UIElements)

    由此,我发现我可以简单地遍历当前加载的 UIElements 列表

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

    所以,就是这样。就在我的鼻子下面……

    【讨论】:

    猜你喜欢
    • 2015-11-24
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 2010-12-12
    相关资源
    最近更新 更多