2014-09-28 74 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

相關問題