Tôi đã tạo hai RadioButton (Trọng lượng và Chiều cao). Tôi sẽ chuyển đổi giữa hai loại. Nhưng họ chia sẻ cùng một ListBox Controllers (listBox1 và listBox2).Làm cách nào để xóa tất cả các mục ListBox?
Có phương pháp nào tốt để xóa tất cả các mục ListBox đơn giản hơn không? Tôi không tìm thấy removeAll() cho ListBox. Tôi không thích phong cách đa dòng phức tạp của mình mà tôi đã đăng ở đây.
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");
}
Tôi thích câu trả lời của Matt Dearing rất nhiều. Nó trông giống như kỹ thuật lập trình tốt hơn cho việc sử dụng cụ thể này, hơn là chỉ thực hiện phương thức Items.Clear(). –