In the previous article, we introduced how to implement grouping panel functions similar to QQ. This time, we will introduce how to implement this function in another way and add animation effects.
In the method described in the previous article, the main technical point is the custom Panel as ItemsPanel. However, this implementation method has two main disadvantages.
1. There is no effect of Virtualizing. Although there are no invisible items.
2. It is not easy to add animation effects.
Here we will introduce another implementation method. It is implemented using Behavior, which is very popular in Blend 3, and can easily add animation effects.
This Behavior principle is very simple, that is, to dynamically calculate the height of items in the ListBox. This requires:
1. different heights cannot be specified for items in ListBox.
2. Specify a default height for each Item.
On the blog of Rooijakkers, I introduced a similar method with high usage. However, the above method did not fully utilize the features of WPF and wrote some unnecessary logic, such as the IsExpanded attribute of the control Expander. There is no animation support.
Here, a simple step to use Behavior is to add a custom Behavior for ListBox, and then set the height of each Item in the SelectionChanged event of ListBox. With Storyboard, you can easily achieve the animation effect.
This Behavior has two custom attributes. One is DefaultHeight and the other is AnimationDuration. As the name suggests, I will not explain it. The core code is as follows.
Core Logic
Private void OnAssociatedObjectSelectionChanged (object sender, SelectionChangedEventArgs e)
{
Double selectedItemFinalHeight = AssociatedObject. ActualHeight;
Storyboard storyBoard = new Storyboard ();
For (int I = 0; I <AssociatedObject. Items. Count; I ++)
{
ListBoxItem item = AssociatedObject. ItemContainerGenerator. ContainerFromIndex (I) as ListBoxItem;
If (! Item. IsSelected)
{
SelectedItemFinalHeight-= DefaultHeight;
DoubleAnimation heightAnimation = new DoubleAnimation ()
{
To = DefaultHeight,
Duration = new Duration (new TimeSpan (0, 0, 0, 0, AnimationDuration ))
};
Storyboard. SetTarget (heightAnimation, item );
Storyboard. SetTargetProperty (heightAnimation, new PropertyPath (FrameworkElement. HeightProperty ));
StoryBoard. Children. Add (heightAnimation );
}
}
// The Padding of the ListBox.
SelectedItemFinalHeight-= 4;
If (AssociatedObject. SelectedIndex> = 0)
{
ListBoxItem selectedItem = AssociatedObject. ItemContainerGenerator. ContainerFromIndex (AssociatedObject. SelectedIndex) as ListBoxItem;
DoubleAnimation fillheightAnimation = new DoubleAnimation ()
{
To = selectedItemFinalHeight,
Duration = new Duration (new TimeSpan (0, 0, 0, 0, AnimationDuration ))
};
Storyboard. SetTarget (fillheightAnimation, selectedItem );
Storyboard. SetTargetProperty (fillheightAnimation, new PropertyPath (FrameworkElement. HeightProperty ));
StoryBoard. Children. Add (fillheightAnimation );
}
StoryBoard. Begin (AssociatedObject );
}
This example is the same as that described in the previous article. To see the animation effect, try it on your own. This Code also contains the previous example.