Description
TreeView has Expanding, Collapsed and ContainerContentChanging events to manipulate the action when the user opens or closes and the TreeViewItem changes IsExpanded property. However, I need to do some animation when the item is "collapsing" (noticed that this event doesn't exist). But, when IsExpanded is changed to false, the TreeView closes at the same time with the node, this way I can't do any animation in the Collapsed event. I tried to use TreeView.Expand(args.Node); but after I can't animate subItems as long as I had it in ContainerContentChanging event only. I used a flag isClosing to control the sequence in the ContainerContentChanging event and I could do the logic, then the animation closed the subItems. But now the problem is: the space that those items had been occupied was still there until I called TreeVeiw.Collapse.
private bool isClosing = false;
private void TreeViewList_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
{
var item = args.ItemContainer;
var itemPanel = item.Content as StackPanel;
if (isClosing)
{
CloseAnimation(itemPanel, INITIAL_TIME, FINAL_TIME, () =>
{
this.Collapse(currentNode);
isClosing = false;
});
}
else
{
OpenAnimation(itemPanel, INITIAL_TIME, FINAL_TIME);
}
}
private void ExpandableList_Collapsed(TreeView sender, TreeViewCollapsedEventArgs args)
{
sender.Expand(args.Node);
isClosing = true;
}
I tried to manipulate the height property too, to avoid the time between the animation item and the closed background panel, but when I change the height element the TreeView gets crazy and the items become smaller when are Expanded a second time.
So, do any tips for doing smooth custom animation to be used when the TreeViewItem is collapsing? I had no options anymore...