Bolalarni StackPanel dan chiqarish

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

Yuqoridagi kodni ishlatganingizdan so'ng. Tekshiruvlar hali ham ekranda. X x 33 ga teng. Shunday qilib, men bolalarni qanday qilib olib tashlayapman?

1
stackPanel.Children.Clear() harakat qilib ko'ring
qo'shib qo'ydi muallif Stanley S, manba
stackPanel.Children.Clear() harakat qilib ko'ring
qo'shib qo'ydi muallif Stanley S, manba

6 javoblar

Yaxshi Minimal, to'liq va tasdiqlanishi mumkin kod misoli bo'lmasa, sizning kodingiz nima ekanligini aniq bilish mumkin emas, uning xatti-harakatlarini tushuntirib berilmasin ishonch bilan.

Biroq, ba'zi bir kuzatishlar:

  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, 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. :)
1
qo'shib qo'ydi
Sizning 100% to'g'ri. Ba'zan men faqatgina to'g'ri ishlarni qilmayman, chunki boshqa variantlardan xabardor emasman. Men sizning 2-bandingizni kuzatib boraman. rahmat
qo'shib qo'ydi muallif lv.programmer, manba

Yaxshi Minimal, to'liq va tasdiqlanishi mumkin kod misoli bo'lmasa, sizning kodingiz nima ekanligini aniq bilish mumkin emas, uning xatti-harakatlarini tushuntirib berilmasin ishonch bilan.

Biroq, ba'zi bir kuzatishlar:

  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, 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. :)
1
qo'shib qo'ydi
Sizning 100% to'g'ri. Ba'zan men faqatgina to'g'ri ishlarni qilmayman, chunki boshqa variantlardan xabardor emasman. Men sizning 2-bandingizni kuzatib boraman. rahmat
qo'shib qo'ydi muallif lv.programmer, manba

stackPanel.Children returns a UIElementCollection so you can just use Clear() like this :

stackPanel.Children.Clear()
1
qo'shib qo'ydi
Nima uchun ularning birma-bir o'chirilishi mumkin emasligini tushuntirib bera olasizmi?
qo'shib qo'ydi muallif Lei Yang, manba
@ lv.programmer javobingizni (yashil tasdiq belgisi) deb belgilaganingizga ishonchingiz komilmi? Sizning obro'ingizdan qat'i nazar, sizning savolingizga javob berishingiz kerak. ( meta.stackexchange.com/questions/5234/& hellip; )
qo'shib qo'ydi muallif grek40, manba
Buni qanday sog'inib qoldim! Sizga katta rahmat.
qo'shib qo'ydi muallif lv.programmer, manba
Buni javob sifatida belgiladim, lekin fikrlarim past bo'lgani uchun uni omma e'tiboriga havola qilmaydi.
qo'shib qo'ydi muallif lv.programmer, manba
Rahmat Grek40 siz haqsiz.
qo'shib qo'ydi muallif lv.programmer, manba
Xavotir olmang, xursand bo'ldim!
qo'shib qo'ydi muallif Stanley S, manba

stackPanel.Children returns a UIElementCollection so you can just use Clear() like this :

stackPanel.Children.Clear()
1
qo'shib qo'ydi
Nima uchun ularning birma-bir o'chirilishi mumkin emasligini tushuntirib bera olasizmi?
qo'shib qo'ydi muallif Lei Yang, manba
@ lv.programmer javobingizni (yashil tasdiq belgisi) deb belgilaganingizga ishonchingiz komilmi? Sizning obro'ingizdan qat'i nazar, sizning savolingizga javob berishingiz kerak. ( meta.stackexchange.com/questions/5234/& hellip; )
qo'shib qo'ydi muallif grek40, manba
Buni qanday sog'inib qoldim! Sizga katta rahmat.
qo'shib qo'ydi muallif lv.programmer, manba
Buni javob sifatida belgiladim, lekin fikrlarim past bo'lgani uchun uni omma e'tiboriga havola qilmaydi.
qo'shib qo'ydi muallif lv.programmer, manba
Rahmat Grek40 siz haqsiz.
qo'shib qo'ydi muallif lv.programmer, manba
Xavotir olmang, xursand bo'ldim!
qo'shib qo'ydi muallif Stanley S, manba

Nima uchun ularning birma-bir o'chirilishi mumkin emasligini ham tushuntirib bera olasizmi?

Siz qaytadan bir xil kollektsiyani o'zgartirganingiz uchun. Shunday qilib, indeks 0 dan elementni olib tashlang va keyin loop o'zgaruvchilarini 1 ga oshirib, indeks 0 da yangi element yo'q, u hech qachon o'chirilmaydi.

Buni orqaga qarab yechib tashlashingiz mumkin. Bu ishlaydi:

for (int i = stackPanel.Children.Count - 1; i >= 0; i--)
{
    stackPanel.Children.Remove(stackPanel.Children[i]);
}
int x = stackPanel.Children.Count;

Yoki @StanleyS tomonidan tavsiya etilgan stackPanel.Clear() usulini chaqirishingiz mumkin.

0
qo'shib qo'ydi

Nima uchun ularning birma-bir o'chirilishi mumkin emasligini ham tushuntirib bera olasizmi?

Siz qaytadan bir xil kollektsiyani o'zgartirganingiz uchun. Shunday qilib, indeks 0 dan elementni olib tashlang va keyin loop o'zgaruvchilarini 1 ga oshirib, indeks 0 da yangi element yo'q, u hech qachon o'chirilmaydi.

Buni orqaga qarab yechib tashlashingiz mumkin. Bu ishlaydi:

for (int i = stackPanel.Children.Count - 1; i >= 0; i--)
{
    stackPanel.Children.Remove(stackPanel.Children[i]);
}
int x = stackPanel.Children.Count;

Yoki @StanleyS tomonidan tavsiya etilgan stackPanel.Clear() usulini chaqirishingiz mumkin.

0
qo'shib qo'ydi