Souvik Basu
Souvik Basu

Reputation: 3139

How to set items order in StackPanel

I have a List of string. e.g. "abc", "pqr", "xyz" in this order. A StackPanel is data bound to this list. I want to display the list in a StackPanel vertically but in reverse order from top to bottom

"xyz"
"pqr"
"abc"

Is there a way to do this in xaml or do I have to reorder my list?

Upvotes: 2

Views: 4871

Answers (3)

Dan Stevens
Dan Stevens

Reputation: 6830

You can replicate a StackPanel using a DockPanel with LastChildFill="False" attribute and setting the Dock attached property on every child element. By using Dock.Right you can stack elements horizontally in reverse order (i.e right-to-left); using Dock.Bottom stacks vertically in reverse order (i.e. bottom-to-top).

For more details and example code, see my answer to StackPanel reverse order - WPF.

Upvotes: 1

Charlie
Charlie

Reputation: 81

This is very hackish, but seems to work. Set the stackpanel's layout transform to rotate 180. Then each child's layout transform also to 180.

<StackPanel Name="pnlLayers">
<StackPanel.LayoutTransform>
    <RotateTransform Angle="180"/>
</StackPanel.LayoutTransform>

(in my case, I had a custom user control as the row)

<UserControl.LayoutTransform>
    <RotateTransform Angle="180"/>
</UserControl.LayoutTransform>

Upvotes: 5

CodingGorilla
CodingGorilla

Reputation: 19842

Yes and No, the StackPanel will display them in the order in which they are enumerated. So you have a couple of options as I see it:

1) Re-order your list

2) Change your binding, or apply a IValueConverter that does the re-order on the fly. This of course requires coding the converter, but once it's written you can re-use it in your XAML as required without having to modify individual windows, code-behinds, etc.

Upvotes: 2

Related Questions