すべてのリストボックス項目を削除するにはどうすればいいですか? 質問する

すべてのリストボックス項目を削除するにはどうすればいいですか? 質問する

2 つの RadioButton (Weight と Height) を作成しました。2 つのカテゴリを切り替えます。ただし、それらは同じ ListBox コントローラー (listBox1 と listBox2) を共有します。

ListBox のすべての項目をより簡単にクリアする良い方法はありますか? ListBox の removeAll() が見つかりませんでした。ここに投稿した複雑な複数行のスタイルが気に入りません。

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");
    }

ベストアンサー1

Winform や Webform の方法と同じではないのですか?

listBox1.Items.Clear();

おすすめ記事