"WPF" Implementation of the group panel in QQ (2)--Add animation

Source: Internet
Author: User

In the previous article, introduced how to achieve similar QQ group panel function. This time, I'll explain how to implement this feature in another way and add animation effects.

In the way described in the previous article, the main technical point is actually the custom panel as Itemspanel. However, there are two main drawbacks to this way of implementation.

1. No virtualizing effect. Although there are no invisible items.

2. Not easy to add animation effect.

Here we'll show you another way to implement it. This is achieved with the very hot behavior of blend 3, and can be easily added to the animation effect.

The behavior principle is very simple, that is, dynamically calculating the height of the item in the listbox. This requires:

1. You cannot specify a different height for the item of the listbox.

2. Specify the height of a default contraction for each item.

A similar approach to height is presented on Rooijakkers's blog. However, the above approach does not take full advantage of the WPF features and writes unnecessary logic, such as the isexpanded properties of the control expander. And there's no animated support.

The simple step here is to add a custom behavior for the listbox, and then set the height of each item in the SelectionChanged event of the listbox. \ behavior The animation effect can be achieved easily with storyboard.

This behavior has two custom attributes. One is DefaultHeight, one is animationduration. As the name suggests, do not explain. The core code is shown below.

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 looks at the screenshot as described in the previous article. To see the animation effect or to try it yourself. In this code, the last example is included.

This article supporting source code

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.