Stworzyłem dwa RadioButton (waga i wysokość). Zrobię przełączenie między dwiema kategoriami. Ale mają te same kontrolery ListBox (listBox1 i listBox2).Jak usunąć wszystkie elementy ListBox?
Czy istnieje dobra metoda do prostszego wyczyszczenia wszystkich elementów ListBox? Nie znalazłem removeAll() dla ListBox. Nie podoba mi się mój złożony styl wielolinii, który tutaj zamieściłem.
private void Weight_Click(object sender, RoutedEventArgs e)
{
// switch between the radioButton "Weith" and "Height"
// Clear all the items first
listBox1.Items.Remove("foot");
listBox1.Items.Remove("inch");
listBox1.Items.Remove("meter");
listBox2.Items.Remove("foot");
listBox2.Items.Remove("inch");
listBox2.Items.Remove("meter");
// Add source units items for listBox1
listBox1.Items.Add("kilogram");
listBox1.Items.Add("pound");
// Add target units items for listBox2
listBox2.Items.Add("kilogram");
listBox2.Items.Add("pound");
}
private void Height_Click(object sender, RoutedEventArgs e)
{
// switch between the radioButton "Weith" and "Height"
// Clear all the items first
listBox1.Items.Remove("kilogram");
listBox1.Items.Remove("pound");
listBox2.Items.Remove("kilogram");
listBox2.Items.Remove("pound");
// Add source units items for listBox1
listBox1.Items.Add("foot");
listBox1.Items.Add("inch");
listBox1.Items.Add("meter");
// Add target units items for listBox2
listBox2.Items.Add("foot");
listBox2.Items.Add("inch");
listBox2.Items.Add("meter");
}
Bardzo lubię odpowiedź Matta Dearinga. Wygląda to na lepszą technikę programowania dla tego konkretnego zastosowania, niż tylko wykonanie metody Items.Clear(). –