wpf 如何更改 Stackpanel 中元素的位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12996983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the position of an element in Stackpanel?
提问by ar.gorgin
I have a Stackpanel with 5 child .
我有一个有 5 个孩子的 Stackpanel。
<StackPanel Orientation="Horizontal">
<TextBlock >1</TextBlock>
<TextBlock >2</TextBlock>
<TextBlock >3</TextBlock>
<TextBlock >4</TextBlock>
<TextBlock >5</TextBlock>
</StackPanel>
I want change the position of child[2].
我想改变孩子的位置[2]。
How do change the position of an element in runtime?
如何在运行时更改元素的位置?
回答by pdvries
It can be achieved by keeping track of the index-element of the Children-property of the StackPanel. I send you some sample code that demonstrates the working of this. For instance, consider the following code:
它可以通过跟踪 StackPanel 的 Children-property 的 index-element 来实现。我向您发送了一些示例代码,演示了它的工作原理。例如,考虑以下代码:
int currentSelectedIndex = stackPanel1.Children.IndexOf(CurrentSelectedTextBlock);
int downIndex = currentSelectedIndex + 1;
int childCount = stackPanel1.Children.Count;
if (downIndex < childCount)
{
stackPanel1.Children.RemoveAt(currentSelectedIndex);
stackPanel1.Children.Insert(downIndex, CurrentSelectedTextBlock);
}
else if (downIndex == childCount)
{
stackPanel1.Children.RemoveAt(currentSelectedIndex);
stackPanel1.Children.Insert(currentSelectedIndex, CurrentSelectedTextBlock);
}
It gets the currently-selected TextBlock and moves its index up one higher. You then need to update the Children-property of the StackPanel by removing and inserting it back again.
它获取当前选择的 TextBlock 并将其索引向上移动一个。然后,您需要通过删除并再次插入来更新 StackPanel 的 Children 属性。
I question whether you want to use a StackPanel for this type of purpose. It's much easier to use an ItemsControl, like a ListBox as they can be bound to a ObservableCollection of T. Once the bound collection is updated, the control is updated likewise.
我质疑您是否想将 StackPanel 用于此类目的。使用 ItemsControl 更容易,比如 ListBox,因为它们可以绑定到 T 的 ObservableCollection。一旦绑定的集合更新,控件也会更新。
I hope this helps. The sample code can be download here.
我希望这有帮助。示例代码可以在这里下载。
回答by pdvries
The question is somewhat unclear as the intention is not laid out very specific. The following code allows you to move a TextBlock based upon the Text-content-property:
这个问题有点不清楚,因为意图不是很具体。以下代码允许您根据 Text-content-property 移动 TextBlock:
string number = "4";
TextBlock textBlockToSearch = null;
foreach (var child in stackPanel1.Children)
{
if (child is TextBlock)
{
var textBlock = (TextBlock) child;
if (textBlock.Text.CompareTo(number) == 0)
textBlockToSearch = textBlock;
}
}
if (textBlockToSearch != null)
{
stackPanel1.Children.Remove(textBlockToSearch);
int pos = 2;
stackPanel1.Children.Insert(pos - 1, textBlockToSearch);
}
else
{
Debug.WriteLine("Could not find TextBlock");
}
If you have other intentions, like using your mouse once selecting the TextBlock, you may need to use different techniques, like seen in the Visual Studio-interface at design-time.
如果您有其他意图,例如在选择 TextBlock 后使用鼠标,您可能需要使用不同的技术,例如在设计时在 Visual Studio 界面中看到的。
Hope this helps.
希望这可以帮助。

