【问题标题】:How to remove all ListBox items?如何删除所有 ListBox 项目?
【发布时间】:2010-04-09 23:53:32
【问题描述】:

我创建了两个 RadioButton(重量和高度)。我会在这两个类别之间切换。但它们共享相同的 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");
    }

【问题讨论】:

  • 我非常喜欢 Matt Dearing 的回答。对于这种特定用途,它看起来像是更好的编程技术,而不仅仅是使用 Items.Clear() 方法。

标签: c# wpf listbox


【解决方案1】:

是不是和Winform和Webform的方式一样?

listBox1.Items.Clear();

【讨论】:

  • 这是一个糟糕的答案,因为您未能解释他不应该首先以这种方式使用Items.Add。请参阅 Matt Dearning 的答案以获得更好的解决方案。
  • 问题是“有没有什么好方法可以更简单地清除所有 ListBox 项?”这是一个简单的问题,它有一个简单的答案!马特的回答是针对“我在这里做正确的事吗?”这个问题
  • 这归结为一种社区责任感。考虑一下:一个八岁的孩子拿着一把上膛的枪(不是玩具)向我走来。他显然没有注意他指向的地方。他问我怎么拍。在这种情况下,“只需扣动扳机”是一个糟糕的答案。更好的答案是解释枪支安全。对于一个八岁的孩子来说,最好的答案可能是把枪拿走,去找他的父母。这里的赌注要低得多,但原则是一样的:鼓励写得不好的代码会造成真正的损害和真正的痛苦。这对我们所有人来说都是真正的代价。
【解决方案2】:

我认为将列表框实际绑定到数据源会更好,因为看起来您正在向每个列表框添加相同的元素。一个简单的例子是这样的:

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

【讨论】:

  • 你甚至可以在 1 行中做:listBox1.ItemsSource = listBox2.ItemsSource = _weight; :)
【解决方案3】:

在 .cs 文件中编写以下代码:

ListBox.Items.Clear();

【讨论】:

    【解决方案4】:
    while (listBox1.Items.Count > 0){ 
        listBox1.Items.Remove(0);
    }
    

    【讨论】:

    • 这本质上是 ListBox.Items.Clear() 所做的吗?
    【解决方案5】:

    您应该可以使用Clear() 方法。

    【讨论】:

      【解决方案6】:

      我是这样制作的,并且对我正常工作:

      if (listview1.Items.Count > 0)
              {
                  for (int a = listview1.Items.Count -1; a > 0 ; a--)
                  {
                      listview1.Items.RemoveAt(a);
                  }
                      listview1.Refresh();
      
              }
      

      解释:使用“Clear()”只会删除项目,不要 然后从对象中删除,使用 RemoveAt() 删除开始位置的项目 只需重新分配其他[如果你删除项目[0],项目[1]变成[0]触发一个新的内部事件], 所以从结尾删除不会影响其他人的位置, 它是一个 Stack 行为,这样我们就可以 Stack 覆盖所有项目,重置对象。

      【讨论】:

      • 我不明白您为什么要询问清除列表框,但您的答案是使用列表视图。
      【解决方案7】:
      • VB ListBox2.DataSource = 没有
      • C# ListBox2.DataSource = null;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-08
        • 2011-07-19
        • 2014-06-09
        相关资源
        最近更新 更多