Skip to main content

Reversing the Z-order on a StackPanel

I had an issue the other day where I have an items control with an arbitrary number of items. I'm using Silverlight 4 and the MVVM pattern. The items to go in my ItemsControl were to be stacked horizontally, but with a negative margin such that the ones on top overlapped the ones below it. Since the items below it in the list get a higher Z-Index I had to reverse it.

I thought the solution I came up with to be nice and thought I'd share. I created the following behavior.

  1. namespace My.Application.Namespace
  2. {
  3.   using System.Windows;
  4.   using System.Windows.Controls;
  5.   using System.Windows.Interactivity;
  7.   public class StackPanelReverseZIndex : Behavior<StackPanel>
  8.   {
  9.     /// <summary>
  10.     /// Handles the onAttached event
  11.     /// </summary>
  12.     protected override void OnAttached()
  13.     {
  14.       base.OnAttached();
  16.       AssociatedObject.LayoutUpdated += new System.EventHandler(AssociatedObject_LayoutUpdated);
  17.     }
  19.     protected void AssociatedObject_LayoutUpdated(object sender, System.EventArgs e)
  20.     {
  21.       int childCount = AssociatedObject.Children.Count;
  22.       foreach (FrameworkElement element in AssociatedObject.Children)
  23.       {
  24.         element.SetValue(Canvas.ZIndexProperty, childCount);
  25.         childCount--;
  26.       }
  27.     }
  29.     /// <summary>
  30.     /// Handles the OnDetaching event
  31.     /// </summary>
  32.     protected override void OnDetaching()
  33.     {
  34.       base.OnDetaching();
  36.       AssociatedObject.LayoutUpdated -= AssociatedObject_LayoutUpdated;
  37.     }
  38.   }
  39. }

And in order to actually apply the behavior, I added the following in XAML of the ItemsControl's panel:

  1. <StackPanel Orientation="Horizontal">
  2.   <interactivity:Interaction.Behaviors>
  3.     <myNamespace:StackPanelReverseZIndex />
  4.   </interactivity:Interaction.Behaviors>
  5. </StackPanel>


Exactly what I was looking for

I had a similar requirement on a recent project and this solution not only works, it's super elegant. I will go forth with a new appreciation of behaviors in WPF.

Nice post and excellent work

Nice post and excellent work by you.

Wonderful information

Reversing is the most difficult thing I have ever done on stock panel. I had to learn a few things from the articles of essayontime review. Your information certainly helped me master the program and am grateful.

Custom Assignment

I cherish the Viking task of yours. It appears to be so pleasant and justifiable. The site has comparable activities. This helped me to Custom Assignment take in a great deal about the subject and make my very own venture.

Loved to read your blog. I

Loved to read your blog. I would like to suggest you that traffic show most people read blogs on Mondays. So it should encourage blogger to write new write ups over the weekend primarily


I unquestionably have started to quantity them in an Buy Custom Paper Online exceed expectations sheet and a hued mark so as that I don’t buy dups. Alpha request is alright to begin yet in a rack trouble like mine it doesnt fine art as a result of the truth.


Spotify for iPhone, free and safe download. Spotify latest version: Find, share and enjoy your favorite music online. Spotify turned the world of online music ...

Directorate of government examination is an individual department, which is under the ministry of secondary education

great one

great one mate, you should also check out spotify premium apk since its an amazing app

Thanks a lot for the great

Thanks a lot for the great tips here, I really enjoyed it a lot. If you are looking for some writing services, then make sure to check out this essay reviews website to be 100% sure that you will avoid fake & scam.

Uttar Pradesh UP Board Class

Uttar Pradesh UP Board Class 12 and Class 10 results are expected to be declared by the first week of June, and students should check their results at official website once the results are declared by the board.

Interesting blog.

I am 100% at your back for this road safety forum you are having. It's a great way of instilling to the people the value of safety when on the road and thus, preventing any accidents from happening. In our country, the rate of minor road accidents seems to rise so this forum would be helpful especially if attended by people mainly using the road for transportation

Lakhs of students appears for

Lakhs of students appears for the Central Board of Secondary Examination every year. This year, around 10,29,874 candidates appeared for CBSE Class 12th XII Board Exam from which the total number of boys are 6,03,064 and the total number of girls are 4,26,810 appeared for the 12th board exam. There has been an increase of 9.32 % in total number of students when compared with the number of students last year in

Maharashtra Board Results

Maharashtra Class 12th HSC examination has been conducted by the Maharashtra State Board of Secondary & Higher Secondary Education (MSBSHSE) at different boards including Pune, Mumbai, Aurangabad, Nasik, Kolhapur, Amravati, Latur, Nagpur and Ratnagiri.

Fabulous article to read with

Fabulous article to read with interesting Nike Air Zoom Vomero 12 Løbesko which is meaning to us in many ways. You can use.

exactly what i've been seeking these days.

this fixed the problem. I know this is crazy but i had some problem. it was not in the order.
anyway here is my site. free amazon gift card codes

greatest moment of site here...

Wow. cool post. I’d like to write like this too – taking time and real hard work to make a great article… but I put

things off too much and never seem to get started. Thanks though. btw Get pokemon magikarp jump hack here
Pokemon magikarp jump Hack

Very educative piece. I have

Very educative piece. I have learnt several news thing from this article. Thanks for your

I liked the information you

I liked the information you have.View this site as the data available will give you a helping hand when you agree to order essays online and to achieve the highest results when it comes to custom essay writing service.

The information provided here

The information provided here was really helpful. Thanks for sharing this outstanding article is helps your academic papers.

Online writing services are

Online writing services are the finest choices to learn the new writing techniques and they open a world of creativity in front of students. Students always need help from experts to score high grades in their academic paper. They believe essay writing service, dissertation writing service etc are the best options to ask for help. Visit our site.


Your post was very nicely written. I’ll be back in the future for sure!

hack google opinion rewards

nicely written. I’ll be back in the future !!
btw visit my site here for google opinion rewards hack


What a good blog you have here. Please update it more often. This topics is my interest. Thank you. . .

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <asp>, <c>, <cpp>, <cs>, <css>, <drupal5>, <drupal6>, <html4strict>, <java>, <javascript>, <jquery>, <php>, <python>, <ruby>, <sql>, <xml>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.