[英]How to remove all ListBox items?
I created two RadioButton (Weight and Height).我创建了两个 RadioButton(重量和高度)。 I will do switch between the two categories.
我会在这两个类别之间切换。 But the they share the same ListBox Controllers (listBox1 and listBox2).
但是它们共享相同的 ListBox 控制器(listBox1 和 listBox2)。
Is there any good method to clear all the ListBox items simpler?有没有什么好的方法可以更简单地清除所有 ListBox 项目? I didn't find the removeAll() for ListBox.
我没有找到 ListBox 的 removeAll()。 I don't like my complex multi-lines style which I posted here.
我不喜欢我在这里发布的复杂的多线样式。
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();
I think it would be better to actually bind your listBoxes to a datasource, since it looks like you are adding the same elements to each listbox.我认为将您的列表框实际绑定到数据源会更好,因为看起来您正在向每个列表框添加相同的元素。 A simple example would be something like this:
一个简单的例子是这样的:
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;
}
Write the following code in the .cs file:在 .cs 文件中写入以下代码:
ListBox.Items.Clear(); ListBox.Items.Clear();
while (listBox1.Items.Count > 0){
listBox1.Items.Remove(0);
}
您应该能够使用Clear()
方法。
I made on this way, and work properly to me:我以这种方式制作,并且对我正常工作:
if (listview1.Items.Count > 0)
{
for (int a = listview1.Items.Count -1; a > 0 ; a--)
{
listview1.Items.RemoveAt(a);
}
listview1.Refresh();
}
Explaining: using "Clear()" erases only the items, do not removes then from object, using RemoveAt() to removing an item of beginning position just realocate the others [if u remove item[0], item[1] turns into [0] triggering a new internal event], so removing from the ending no affect de others position, its a Stack behavior, this way we can Stack over all items, reseting the object.解释:使用“Clear()”只擦除项目,不从对象中删除 then,使用 RemoveAt() 删除开始位置的项目只是重新分配其他项目[如果你删除项目[0],项目[1]变成[0] 触发一个新的内部事件],所以从结尾移除对其他位置没有影响,它是一个 Stack 行为,这样我们可以堆叠所有项目,重置对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.