简体   繁体   English

如何在 StackPanel 中设置项目顺序

[英]How to set items order in StackPanel

I have a List of string.我有一个字符串列表。 eg "abc", "pqr", "xyz" in this order.例如“abc”、“pqr”、“xyz”的顺序。 A StackPanel is data bound to this list. StackPanel 是绑定到此列表的数据。 I want to display the list in a StackPanel vertically but in reverse order from top to bottom我想在 StackPanel 中垂直显示列表,但从上到下的顺序相反

"xyz"
"pqr"
"abc"

Is there a way to do this in xaml or do I have to reorder my list?有没有办法在 xaml 中执行此操作,还是我必须重新排序我的列表?

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. 将堆栈面板的布局转换设置为旋转180。然后,每个孩子的布局转换也转换为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>

Yes and No, the StackPanel will display them in the order in which they are enumerated. 是和否, StackPanel将按枚举顺序显示它们。 So you have a couple of options as I see it: 因此,您有几种选择,如我所见:

1) Re-order your list 1)重新排序您的清单

2) Change your binding, or apply a IValueConverter that does the re-order on the fly. 2)更改绑定,或应用IValueConverter即时进行重新排序。 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. 当然,这需要对转换器进行编码,但是一旦编写了转换器,您就可以根据需要在XAML中重新使用它,而无需修改单个窗口,代码隐藏等。

You can replicate a StackPanel using a DockPanel with LastChildFill="False" attribute and setting the Dock attached property on every child element.您可以使用具有LastChildFill="False"属性的DockPanel复制StackPanel ,并在每个子元素上设置Dock附加属性。 By using Dock.Right you can stack elements horizontally in reverse order (ie right-to-left);通过使用Dock.Right ,您可以以相反的顺序(即从右到左)水平堆叠元素; using Dock.Bottom stacks vertically in reverse order (ie bottom-to-top).使用Dock.Bottom以相反的顺序垂直堆叠(即从下到上)。

For more details and example code, see my answer to StackPanel reverse order - WPF .有关更多详细信息和示例代码,请参阅我对StackPanel 反向顺序的回答 - WPF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM