重新排序StackPanel内的孩子?在Win8

时间:2012-11-19 07:04:34

标签: c# windows-8 windows-store-apps windows-store

我通过删除并在所需位置添加元素(插入方法)来交换堆栈面板中的两个子项。但是,一旦我访问元素,删除和添加元素会导致内存地址问题和应用程序崩溃swapped。在StackPanel没有“删除和添加”的情况下我还有其他任何方法可以实现交换吗?

的Win8 + XAML + C#。

代码段:

    UIElementCollection children = stack_map.Children;

    int i = children.IndexOf(stack_listView);                                
    StackPanel sp = children[i] as StackPanel;

    int j = children.IndexOf(mapPageInstance);                                
    MapPage mp = children[j] as MapPage;

    stack_map.Children.Remove(sp);
    stack_map.Children.Insert(1, sp);

我想将'sp'从零索引定位到stack_map中的第一个索引。

'mapPageInstance'是自定义Map类的实例(基本上是地图)

1 个答案:

答案 0 :(得分:0)

要交换上面堆栈面板中的元素,我只需使用以下内容:

stack_map.Children.Remove(stack_listView);
stack_map.Children.Insert(1, stack_listView);

当然,这意味着您的堆叠面板中有多个项目。

如果继续按名称而不是索引访问stackpanel项目,则交换后应该没有任何问题/内存错误

相关问题