从 StackPanel 中删除子项 [英] Removing Children from a StackPanel

查看:45
本文介绍了从 StackPanel 中删除子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for(int i = 0; i < stackPanel.Children.Count; i++)
{
     stackPanel.Children.Remove(stackPanel.Children[i]);
}
int x  = stackPanel.Children.Count();

运行上述代码后.控件仍在屏幕上.那么 x 等于 33.那么我如何真正删除孩子?

After running the above code. The controls are still on the screen. Then x equals 33. So how do I really remove the children?

推荐答案

没有一个好的最小、完整和可验证的代码示例,不可能确切地知道您的代码在做什么,更不用说确定地解释其行为了.

Without a good Minimal, Complete, and Verifiable code example, it's impossible to know exactly what your code is doing, never mind explain its behavior with certainty.

但是,这里有一些观察:

However, here are some observations:

  1. 根据您发布的代码,我希望 一些 孩子 被删除.但是因为您在删除子项的同时改变了用于标识要删除的子项的索引,所以您跳过了一半的子项.例如.当 i 为 0 时,删除第一个孩子.但是这样做会导致第二个孩子现在成为第一个孩子.然后你增加 i.因此,下一次循环时,您不会删除最初是第二个孩子的内容,而是删除最初是第三个孩子的内容.等等.

    您可以通过在最后开始索引并向后工作来解决此问题.或者总是删除索引 0 处的第一个孩子,直到集合为空.无论哪种方式,您还可以通过使用 RemoveAt() 方法来提高效率,该方法删除特定索引处的元素,而不是强制集合搜索直到找到该元素.

    当然,还有 Clear() 方法.因此,如果您想一次删除所有元素,您可以使用它而不是自己编写循环.
  2. 您真的不应该首先自己操作 UIElementCollection.如果你有这样的动态元素,你应该创建一个视图模型类来表示 StackPanel 中的每个元素,创建该类的实例并将它们添加到例如一个 ObservableCollection,然后将该集合绑定到使用 StackPanelItemsControlItemsSource 作为ItemsPanel(默认情况下 ItemsControl 会这样做).

    完成后,您只需根据需要操作集合以添加/删除/重新排列元素.当然,这样做是为了避免您在这里犯的错误.:)
  1. Based on the code you posted, I would expect that some children are removed. But because you are removing children at the same time you are varying the index used to identify the children to remove, you are skipping half the children. E.g. when i is 0, you remove the first child. But doing so causes the second child to now be the first child. Then you increment i. So the next time through the loop, you don't remove what was originally the second child, you remove what was originally the third child. And so on.

    You could address this by starting your index at the end, and working backwards. Or by just always removing the first child, at index 0, until the collection is empty. Either way, you can also improve efficiency by using the RemoveAt() method, which removes an element at a specific index, rather than forcing the collection to search until finding the element.

    Of course, there's also the Clear() method. So if you want to remove all of the elements at once, you can use that instead of writing the loop yourself.
  2. You really shouldn't be manipulating the UIElementCollection yourself in the first place. If you have dynamic elements like this, you should instead create a view model class to represent each element in the StackPanel, create instances of that class and add them to e.g. an ObservableCollection<T>, and then bind that collection to the ItemsSource of an ItemsControl that uses a StackPanel as the ItemsPanel (which ItemsControl does by default).

    Having done that, you then just manipulate the collection as needed to add/remove/rearrange elements. Of course, doing so in a way to avoids the mistake you've made here. :)

这篇关于从 StackPanel 中删除子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆