[WPF] implement grouping panel in QQ (2) -- add an animation

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.