views:

12

answers:

1

Is there any way to tell when the containers are finished being made for a ListView?

A detailed explanation of what I've done so far

I have a ListView control that has a DataTemplate in one of its columns that contains a CheckBox Control.. I've figured out how to access the CheckBox dynamically using the object that the ListView is bound to.

                ListViewItem lItem = (ListViewItem)ListView.ItemContainerGenerator.ContainerFromItem(trackToHandle);
                CheckBox checkBoxToHandle = FindChild<CheckBox>(lItem, "CheckBox");

The problem is that the CheckBoxes "reset" (become unchecked) whenever I scroll too far or whenever I sort the columns. I figured out this was because the VirtualizingStackPanel was only spitting out containers for those ListViewItems that were visible (or almost visible).. And because the CheckBox is inside a DataTemplate that is defined in the XAML it gets thrown away everytime it goes out of view or when the list is sorted.

I got around this by creating a separate list of CheckBoxes and using the actual CheckBoxes "click" event to change the state of the corresponding CheckBox in my list.. then made a little method to go change the state of all the visible CheckBoxes whenever the user scrolls... as a result it appears like it should have in the first place.

Except when I sort the columns. I tried making it re-do the CheckBoxes (like before) right after it'd sorted a column but it didn't work. My best guess is that it doesn't immediately make the containers after I sort..

Is there any way to tell when the containers are finished being made for a ListView?

A: 

If you bind your checkboxes IsChecked property to a boolean property on your data context, then you will not have this issue.

The whole purpose of the VirtualizingStackPanel is reduce memory usage by not creating ListItem's unless needed.

In effect, you need to move the data side of the checkbox away from the control.

benPearce
Wow, that worked really well. Thanks a lot.. I was trying to do all this extra stuff and that solved it with not even an extra line of XAML. Thanks a lot man
Ryan