I'm writing a attached behavior
public class resizingBehavior : Behavior<ItemsControl>
to determine the number of visible items in a treeview.
In OnAttached Method
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Loaded += AssociatedObject_Loaded;
}
Inside AssociatedObject.Loaded
event handler
I'm hooking a size changed event handler for my treeview control
private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
{
var itemsControl = (ItemsControl)sender;
_sizeChangedEventHandler = (s, f) => ComputeVisibleItemsCount(itemsControl);
var treeListView = AssociatedObject as TreeListView;
treeListView.SizeChanged += _sizeChangedEventHandler;
itemsControl.Loaded -= AssociatedObject_Loaded;
}
These events gets called when I load my app and when I resize my treeview. My problem is in the calculation of visible items inside the treeview. Unfortunately the IsVisible property doesnot give me a correct result. I used the answer suggested in In WPF, how can I determine whether a control is visible to the user?
But that does not seems to work for me. It gives me a wrong result.
Any good practice or standard solution to determine the actual viewable items in a itemscontrol ? when resizing and scrolling ? By the way I'm using reactive collection for my treeview items.