Monday, November 17, 2008

In my previous post, I showed an approach to getting the expander to ‘reveal’ its content. Although it worked well, it did so by programmatically creating a storyboard and targeting the height/width. That did not sit well with me, and in this post I’ll show another approach.

Before I had settled on that approach, I had created a dependency property on Expander ‘ExpandAmount’ but quickly realized I could not target it from within the template. Since you also can not target custom attached properties in a storyboard, I was stuck. However, the solution is as clear as it is simple: Just create a custom ContentControl that has our beloved ‘ExpandAmount’ property. We will be able to target that property for sure!

The template becomes:

image

Note that I set the Height and Width to 0, because the expander always begins in ‘collapsed’ state. Visibility is not set to collapsed.
We can target it in our –now Blend friendly- VSM:

image

The relevant code on our ExpandableContentControl is shown here:

image

Since content can be either a UIElement or a regular object (like String), we can not use it to determine the size we would like to be. However, we can take a look at the contentpresenter that is presenting our content and ask it. By doing this in the OnApplyTemplate, we will not be able to respond to changes inside the contenttemplate. However, for our demonstration, that is not a big issue.

One issue I ran into was the fact that this control has no nice way to get to the expander and find out how it should do calculate its Size. Based on the ExpandDirection, we might want to change the width or the height. I chose to have the Expander tell its content when it starts to expand or collapse.

We end up with a fully customizable expander. Go have some fun in Blend. Below is a sample where I adjusted the speed to slow down at the end. Also included is the source.

 

Source

Wednesday, November 19, 2008 9:33:01 AM (Romance Standard Time, UTC+01:00)
Missing the source code.
Fallon Massey
Wednesday, November 19, 2008 6:17:43 PM (Romance Standard Time, UTC+01:00)
Sorry, that is fixed now.
Ruurd
Wednesday, January 07, 2009 1:32:39 AM (Romance Standard Time, UTC+01:00)
Just a question, when I use this (I'm using it in an accordion fashion) - The smooth expander starts expanded always, and will immediately collapse. How can I start with the initial state being closed?

Also, the IsExpanded property doesn't work anymore. (usually I would set IsExpanded="False", but I can't do that)
Kori Francis
Wednesday, January 07, 2009 7:44:49 AM (Romance Standard Time, UTC+01:00)
Hi Kori,

The percentage of shown is ExpandAmount. If you want it to start closed, I would suggest transitioning to the Collapsed state inside of the OnApplyTemplate override.

This code should really be seen as a small spike, so I do not know if I hooked up the IsExpanded property. Still, I presume it is still hooked up and the one triggering the state changed (to Collapsed or Expanded). Have you changed the template very much?

You will be happy to know I'm working on a full featured accordion using the technique described in the post, so you're certainly on the correct track.
Ruurd
Thursday, January 15, 2009 4:57:57 PM (Romance Standard Time, UTC+01:00)
Thanks for this, but when running the solution there's endless exceptions ("A first chance exception of type 'System.NullReferenceException' occurred in System.Windows.dll") and the CPU gets pegged..
James
Saturday, January 17, 2009 9:29:43 AM (Romance Standard Time, UTC+01:00)
James: did change the template or the source in any way? The source was used to create the samples you see running inline in the blog, so at that point it was running just fine.
Could you write down the silverlight version you are running (seems very unlikely to be a problem, but still).

Thanks,
rj
Ruurd
Monday, May 25, 2009 7:32:11 AM (Romance Standard Time, UTC+01:00)
No Source Code avail
Amar
Tuesday, November 10, 2009 9:10:16 AM (Romance Standard Time, UTC+01:00)
It’s a very useful application. I have downloaded a similar one by means of <a href=http://rapid4me.com> rapidshare search engine </a> , but it somehow didn’t work. Does it make any difference, where I download it?
colier
Thursday, November 12, 2009 4:41:40 AM (Romance Standard Time, UTC+01:00)
Hello everyone, im new here, my name is Distance education master game theory, i have been reading your site for long, a very nice community.
I am from Burundi and now teach English, please tell me right I wrote the following sentence: "It means recent lead relations and appears with the potential of driving years."

With love :-(, Distance education master game theory.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):