如何在 StackPanel 中设置项目顺序
我有一个字符串列表。例如,按此顺序“abc”、“pqr”、“xyz”。 StackPanel 是绑定到该列表的数据。 我想在 StackPanel 中垂直显示列表,但从上到下以相反的顺序
"xyz"
"pqr"
"abc"
有没有办法在 xaml 中执行此操作,或者我是否必须重新排序我的列表?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这非常hackish,但似乎有效。将 stackpanel 的布局变换设置为旋转 180 度。然后每个子级的布局变换也为 180 度。
(就我而言,我有一个自定义用户控件作为行)
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.
(in my case, I had a custom user control as the row)
是和否,
StackPanel
将按照枚举顺序显示它们。因此,在我看来,您有几个选择:1)重新排序列表
2)更改绑定,或应用
IValueConverter
即时进行重新排序。这当然需要对转换器进行编码,但一旦编写完成,您就可以根据需要在 XAML 中重复使用它,而无需修改各个窗口、代码隐藏等。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.您可以使用带有
LastChildFill="False"
属性的DockPanel
复制StackPanel
,并设置Dock
附加属性每个子元素。通过使用Dock.Right
,您可以以相反的顺序(即从右到左)水平堆叠元素;使用Dock.Bottom
以相反的顺序垂直堆叠(即从下到上)。有关更多详细信息和示例代码,请参阅我对 StackPanel 逆序 - WPF 的回答。
You can replicate a
StackPanel
using aDockPanel
withLastChildFill="False"
attribute and setting theDock
attached property on every child element. By usingDock.Right
you can stack elements horizontally in reverse order (i.e right-to-left); usingDock.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.