Я создал два RadioButton (Вес и Высота). Я сделаю переключатель между этими двумя категориями. Но они совместно используют те же Контроллеры ListBox (listBox1 и listBox2).
Там какой-либо хороший метод состоит в том, чтобы очистить все более простые объекты ListBox? Я не нашел removeAll () для ListBox. Мне не нравится мой сложный стиль мультилиний, который я отправил здесь.
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");
}
разве это не то же самое, что способ Winform и Webform?
listBox1.Items.Clear();
Я думаю, что было бы лучше привязать ваши списки списков к источнику данных, поскольку похоже, что вы добавляете одни и те же элементы в каждый список. Простым примером может быть что-то вроде этого:
private List<String> _weight = new List<string>() { "kilogram", "pound" };
private List<String> _height = new List<string>() { "foot", "inch", "meter" };
public Window1()
{
InitializeComponent();
}
private void Weight_Click(object sender, RoutedEventArgs e)
{
listBox1.ItemsSource = _weight;
listBox2.ItemsSource = _weight;
}
private void Height_Click(object sender, RoutedEventArgs e)
{
listBox1.ItemsSource = _height;
listBox2.ItemsSource = _height;
}
while (listBox1.Items.Count > 0){
listBox1.Items.Remove(0);
}
Вы должны иметь возможность использовать Clear ()
метод.