Friday, November 14, 2008

A few weeks ago, we released the Silverlight control toolkit. There are some nice controls in there, including the expander.
In this post I will take a look at the expander and alter is so it’s content transitions in/out. Yes, my peeps: we are extending the expander!

In a simple scenario, the expander is used as follows:

image

I have used a border to show off its content. When you run this program, and click on the expander, this is what you will see:

image

Now, when you click the expander button (helpfully pointed out by my big red arrow!), the expander will immediately remove our content. There is no nice effect or transition going on.
This is consistent with WPF behavior.

But it is not cool, is it?

So, let’s take a better look, and open up the expander.xaml inside of our toolkit source. The relevant part is here:

image

and in the visual states:

image

So, the ‘ExpandSite’ is an element that has a visibility of Collapsed. When the control is ‘expanded’ the expandsite will be set to visible. Effective, but no fun.

We will manually transition the height of the content. I will leave it up to you to create a switch on the expanddirection and animate the width instead.

In order to hook everything up, I will create a new class, called: SmoothExpander.

image

The code should point you to the next change: I have added a scrollviewer inside the new template. So, I copied the expander template and put it inside themes\generic.xaml. The relevant parts are:

image

and

image

First off, I removed the Storyboard in the VisualState, since I will be creating on in code. Also, I have put a scrollviewer around my contentcontrol. Let’s take a look at that in more detail:

  • The scrollviewer will host the contentcontrol
  • Its height is set to 0, so it will not show up
  • The visibility of the contentcontrol is set to Visible

This gives us the option of using the extentHeight property of the Scrollviewer to figure out how large our contentcontrol wants to be!

So, lets implement our behavior:

image

I have been wanting to show how to use a storyboard in code for some time.
In both cases a storyboard is created with a DoubleAnimation that animates from the current Height to either 0 or the intended height. We couple this animation to our scrollViewer and off we go.

Since we do not collapse or expand straight away, we should change our code to run those methods on the storyboard finish:

image

 

Running sample is below. The second expander is of type ‘SmoothExpander’.

Source for this sample

I kept the sample as straightforward as possible. Extra work that should be done though:

  1. Create a templatepart attribute so that Blend knows about this scrollviewer
  2. Do not rely on the existence of the scrollviewer.
  3. Use expanddirection to toggle behavior.
  4. Possibly allow for a maximum to be set on the scrollviewer.
Monday, August 03, 2009 7:47:28 PM (Romance Standard Time, UTC+01:00)
a) your source code is broken
b) nice article
c) animation.completed event handler should be there before begin()
whatever
Sunday, August 09, 2009 6:13:22 PM (Romance Standard Time, UTC+01:00)
a) I have changed the toolkit control slightly, the source in this article has not been updated.
b) thanks!
c) quite right, although you won't run into problems.

thanks for the feedback!
Ruurd Boeke
Thursday, November 12, 2009 1:37:53 AM (Romance Standard Time, UTC+01:00)
Hi. I don't like composers who think. It gets in the way of their plagiarism.
I am from Tuvalu and learning to read in English, give true I wrote the following sentence: "Following this motional boundary, postings went in a one home kind."

:p Thanks in advance. Accessible distance education.
Sunday, December 06, 2009 11:50:28 AM (Romance Standard Time, UTC+01:00)
Give please. A lifetime of happiness! No man alive could bear it: it would be hell on earth.
I am from Republic and , too, and now am writing in English, tell me right I wrote the following sentence: "Insurance auto, it is prior first in the auto to intrigue the policy without having auto fact."

With love :D, Auto insurance price.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):