ItemContainerGenerator.ContainerFromIndex 插入后返回 null [英] ItemContainerGenerator.ContainerFromIndex returns null after Insert

查看:21
本文介绍了ItemContainerGenerator.ContainerFromIndex 插入后返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows Phone 应用中有一个 ListBox.在按钮操作中,我需要在名为 lbListBox 中的每个 ListBoxItem 上设置转换和名称.

I have a ListBox in a Windows Phone app. In a button action I need to set a transformation and name on every ListBoxItem in the ListBox called lb.

我的数据源是

var items = new ObservableCollection<string>();
for (int i = 0; i < 10; ++i)
{
    items.Add("Item " + i);
}
lb.ItemsSource = items;

我有一个代码可以为 ListBox 中的每个 ListBoxItem 添加一个 RenderTransform

I have a code to add a RenderTransform to each ListBoxItem in the 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
}

它工作正常.问题是我首先需要将 item 插入到列表中.当我在 for 循环之前调用 items.Insert(index,"test") 时,当 i==index 时,我得到一个异常,该项目为空.当我插入新项目时无关紧要,我总是为该项目获得空值.

and it works ok. The problem is that I first need to insert and item to the list. When I call items.Insert(index,"test") before the for loop I get an exception that the item is null when i==index. It does not matter when I insert the new item, I always get null for that item.

我做错了什么?或者在尝试访问 ListBoxItem 之前插入新项目时是否需要等待 ListBox 的事件?

What am I doing wrong? Or is there an event of the ListBox I need to wait for when I insert the new item before trying to acces the ListBoxItem?

编辑:我提取了代码并将其放入解决方案中:https://dl.dropboxusercontent.com/u/73642/PhoneApp2.zip.我首先将一个假项目插入到新解决方案中,将其淡出并使用动画将原始项目移动到该位置.

Edit: I extracted the code and put it into a solution: https://dl.dropboxusercontent.com/u/73642/PhoneApp2.zip. I first insert a fake item to the new solution, the fade it away and move the original item to that position using an animation.

推荐答案

等待 Dispatcher 完成其正在执行的操作,例如(由于添加了新项目而更新 UI)

Waiting for the Dispatcher to finish doing what its doing such as (updating the UI because of a new item being added)

this.Dispatcher.BeginInvoke(() =>
{
   //Code Here
});

如果您在 UI 未更新的情况下操作 UI(例如将项目添加到列表框),您将无法运行针对 UI 的代码.

If you ever manipulate the UI such as adding an item to a listbox without the UI getting updated, you will not be able to run code targeting the UI.

这是您的项目开始工作的代码

Here is the code for your project to get working

 private void Button_Click(object sender, RoutedEventArgs e)
    {            
        start = Int32.Parse(from.Text);
        end = Int32.Parse(to.Text);

        fake = items[start];
        //items.Insert(end, fake);

        this.Dispatcher.BeginInvoke(() =>
        {
            for (int i = 0; i < items.Count; ++i)
            {
                var item = this.lb.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
                item.Name = i.ToString();
            }


            (this.lb.ItemContainerGenerator.ContainerFromIndex(end) as ListBoxItem).RenderTransform = new CompositeTransform();
            (this.lb.ItemContainerGenerator.ContainerFromIndex(end) as ListBoxItem).Name = "listBoxItem1";


            (this.lb.ItemContainerGenerator.ContainerFromIndex(start) as ListBoxItem).Name = "listBoxItem";

            sbListBox.Begin();
        });

    }

这篇关于ItemContainerGenerator.ContainerFromIndex 插入后返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆