In WPF, accessing containers within ListBox
- by Jim
I'm creating a DerivedListBox : ListBox and a DerivedHeaderedContentControl : HeaderedContentControl, which will serve as a container for each item in the ListBox.
In order to calculate the size available for the expanded content of the DerivedHeaderedContentControls, I am storing each container object in a list within the DerivedListBox. This way I can calculate the height of the headers of each DerivedHeaderedContentControl and subtract that from the total size available to the DerivedListBox. This would be the size available for the expanded content of a DerivedHeaderedContentControl.
public class DerivedHeaderedContentControl : HeaderedContentControl
{
// Do some binding to DerivedListBox to calculate height.
}
public class DerivedListBox : ListBox
{
private List<DerivedHeaderedContentControl> containers;
protected override DependencyObject GetContainerForItemOverride()
{
DerivedHeaderedContentControl val = new DerivedHeaderedContentControl();
this.containers.Add(val);
return val;
}
// Do some binding to calculate height available for an expanded
// container by iterating over containers.
}
The problem comes in when the DerivedListBox's ItemsSource is cleared (or an item in the items source is removed). How can I determine when the ItemsSource is cleared so that I can clear the containers list?