76279

ItemContainerGenerator.ContainerFromIndex returns null after Insert

Question:

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.

My datasource is

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

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 }

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.

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?

<strong>Edit</strong>: I extracted the code and put it into a solution: <a href="https://dl.dropboxusercontent.com/u/73642/PhoneApp2.zip" rel="nofollow">https://dl.dropboxusercontent.com/u/73642/PhoneApp2.zip</a>. 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.

Answer1:

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

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.

Edit: 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(); }); }

Answer2:

Right after item added there is not container generated because of asynchronous nature of UI subsystem. Try subscribing on the ItemsChanged (or StatusChanged, sorry i don't remember) and get item when event is fired with proper event args.

Recommend

  • localizable.strings - works in simulator but not on device
  • Possible to “watch” both HAML and SASS at the same time?
  • How to specify input and output paths from cmd.exe for a PowerShell script?
  • Reading a file into a multidimensional array
  • Object and struct member access and address offset calculation
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Hardware Accelerated Image Scaling in windows using C++
  • Magento Fatal error: Maximum execution error solution, on WAMP
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • PHP - How to update data to MySQL when click a radio button
  • req.body is undefined - nodejs
  • Can Jackson SerializationFeature be overridden per field or class?
  • How to get a value (ex: baseURL) in every Karate feature?
  • How to recover from a Spring Social ExpiredAuthorizationException
  • angularjs unit test when to use $rootScope.$new()
  • How to set/get protobuf's extension field in Go?
  • Submit form in a displaytag pagination
  • ILMerge & Keep Assembly Name
  • Running a C# exe file
  • Join two tables and save into third-sql
  • How to handle AllServersUnavailable Exception
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • How to model a transition system with SPIN
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Large data - storage and query
  • ORA-29908: missing primary invocation for ancillary operator
  • WOWZA + RTMP + HTML5 Playback?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • How to delete a row from a dynamic generate table using jquery?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • using HTMLImports.whenReady not working in chrome
  • Turn off referential integrity in Derby? is it possible?
  • Authorize attributes not working in MVC 4
  • Add sale price programmatically to product variations
  • EntityFramework adding new object to nested object collection
  • Unable to use reactive element in my shiny app
  • Converting MP3 duration time
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How do I use LINQ to get all the Items that have a particular SubItem?