Search code examples
c#wpfstackpanel

How to add a child element to the top of StackPanel?


I'm trying to add some child elements of StackPanel on top.

There is a code:

<StackPanel Orientation="Vertical">
  <Border Background="SkyBlue">
    <TextBlock>Stacked Item #1</TextBlock>
  </Border>
  <Border Background="CadetBlue">
    <TextBlock>Stacked Item #2</TextBlock>
  </Border>
  <Border Background="LightGoldenRodYellow">
    <TextBlock >Stacked Item #3</TextBlock>
  </Border>

and there is a image:

enter image description here

I want the child elements to be added up instead of down, how can I do this?

Like this:
Stacked Item 3
Stacked Item 2
Stacked Item 1


Solution

  • I found the solution.

    StackPanel.Children.Insert(0, UIElement)
    

    This will insert child element at the head of the list.