我在Windows Phone應用程序中有ListBox
。在按鈕操作中,我需要在名爲lb
的ListBox
中的每個ListBoxItem
上設置轉換和名稱。在插入之後ItemContainerGenerator.ContainerFromIndex返回null
我的數據源是
var items = new ObservableCollection<string>();
for (int i = 0; i < 10; ++i)
{
items.Add("Item " + i);
}
lb.ItemsSource = items;
我有一個代碼爲RenderTransform
ListBox中的
for (int i = 0; i < items.Count;++i)
{
var item = this.lb.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
item.RenderTransform = new CompositeTransform();
item.Name = i.ToString() //needed for storybord
//another stuff
}
添加到每個ListBoxItem
,它工作正常。問題是我首先需要插入並將項目添加到列表中。當我在for循環之前調用items.Insert(index,"test")
時,出現i==index
時產品爲空的異常。插入新項目時無關緊要,我總是爲該項目獲取空值。
我在做什麼錯?或者在我嘗試訪問ListBoxItem
之前插入新項目時,是否需要等待ListBox
的事件?
編輯:我提取了代碼並將其放入一個解決方案:https://dl.dropboxusercontent.com/u/73642/PhoneApp2.zip。我首先在新解決方案中插入一個假項目,將其淡入淡出,並使用動畫將原始項目移動到該位置。
'我有一個代碼添加一個RenderTransform到列表框中的每個ListBoxItem你應該這樣做在XAML。這就是XAML的用途。在基於XAML的技術中使用過程代碼來操作UI元素或它們的屬性是非常令人沮喪的,並且引入了這些類型的錯誤以及所有類型的可維護性問題。 –
我不這麼認爲。如何在綁定時在XAML中設置ListBoxItem的Name或RenderTransform? –
你想要什麼名字? 'RenderTransform'可以通過'Style TargetType =「ListBoxItem」'應用。 –