[英]StackPanel reverse order - WPF
I am trying to reverse the order of which the children appear in the StackPanel.我正在尝试颠倒子项出现在 StackPanel 中的顺序。 I want to be able to add new children (whilst running the app) to the top of the list instead of the bottom.
我希望能够将新的孩子(在运行应用程序时)添加到列表的顶部而不是底部。 I have tried using various XAML code but nothing worked.
我尝试使用各种 XAML 代码,但没有任何效果。
What is the easiest way to do this?最简单的方法是什么?
I've found a way to stack elements in opposite order than they are listed as children using a DockPanel
.我找到了一种方法,以相反的顺序堆叠元素,而不是使用
DockPanel
将它们列为子元素。 This should allow you do what you want entirely in XAML (no need for code-behind).这应该允许你完全在 XAML 中做你想做的事(不需要代码隐藏)。 This could be helpful if you're binding to a collection and you don't want to reverse the order of the collection or don't want to insert items at the head of the collection.
如果您要绑定到一个集合并且不想颠倒集合的顺序或不想在集合的开头插入项目,这可能会有所帮助。
When you specify the Dock
attached property on multiple child element (eg DockPanel.Dock="Bottom"
) it will stack them on the specified edge of the panel in order, such that the first child element will be closest that edge.当您在多个子元素(例如
DockPanel.Dock="Bottom"
)上指定Dock
附加属性时,它将按顺序将它们堆叠在面板的指定边缘上,这样第一个子元素将最接近该边缘。
Therefore, to stack elements bottom-to-top, set the Dock
property to Bottom
on all the child elements and give them a fixed height.因此,要从下到上堆叠元素,请将所有子元素的
Dock
属性设置为Bottom
并为它们指定固定高度。 Here's an example:这是一个例子:
<UserControl x:Class="UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" d:DesignHeight="200" d:DesignWidth="200">
<DockPanel LastChildFill="False">
<Button DockPanel.Dock="Bottom" Height="40" Content="B 1" />
<Button DockPanel.Dock="Bottom" Height="40" Content="B 2" />
<Button DockPanel.Dock="Bottom" Height="40" Content="B 3" />
</DockPanel>
</UserControl>
Note the use of LastChildFill="False"
- by default the DockPanel
will stretch the last item to fill the remaining space.请注意
LastChildFill="False"
的使用 - 默认情况下, DockPanel
将拉伸最后一项以填充剩余空间。
This renders as follows:这呈现如下:
The same also works for right-to-left ordering of elements这同样适用于元素从右到左的排序
<UserControl x:Class="DigitalElectronics.UI.Controls.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" d:DesignHeight="200" d:DesignWidth="200">
<DockPanel LastChildFill="False">
<Button DockPanel.Dock="Right" Width="40" Content="B 1" />
<Button DockPanel.Dock="Right" Width="40" Content="B 2" />
<Button DockPanel.Dock="Right" Width="40" Content="B 3" />
</DockPanel>
</UserControl>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.