求教listview删除选定项问题
while (this.myListBox.SelectedItems.Count != 0){
this.myListBox.Items.Remove(this.myListBox.SelectedItems[0]);
}
我找网上找的,人家说可以用,但是我这说“当 ItemsSource 正在使用时操作无效。改用 ItemsControl.ItemsSource 访问和修改元素。”
这个怎么改阿?大侠们 --------------------编程问答-------------------- for (int i = listView1.SelectedItems.Count - 1; i >= 0; i--)
{
ListViewItem item =listView1.SelectedItems[i];
listView1.Items.Remove(item);
}
listView1.Refresh(); --------------------编程问答-------------------- 用别人的东西,首先你得自己弄清楚每一句的含义,别盲目的使用。 --------------------编程问答-------------------- 报错啊,myListBox4.SelectedItems[i];为Object类型,缺少强制转换 --------------------编程问答-------------------- for(int i=LVCompany.Items.Count - 1; i >= 0;i--)
{
if (this.LVCompany.Items[i].Checked == false)
continue;
this.LVCompany.Items[i].Remove();
}
这个是根据是否选中为条件来进行删除的 --------------------编程问答-------------------- this.listView1.BeginUpdate();
if (this.listView1.Items > 0)
{
foreach (ListViewItem item in this.listView1.SelectedItems)
{
this.listView1.Items.Remove(item);
}
}
this.listView1.EndUpdate();
或直接
this.listView1.SelectedItems.Clear(); --------------------编程问答-------------------- if (this.myListBox.SelectedItems != null && this.myListBox.SelectedItems.Count != 0)
{
this.myListBox.Items.Remove(this.myListBox.SelectedItems[0]);
} --------------------编程问答-------------------- for (int i = this.myListBox.Items.Count - 1; i >= 0; i--)
{
if (this.myListBox.GetSelected(i))
{
this.myListBox.Items.RemoveAt(i);
}
}
安心用吧,关键点在于倒序 --------------------编程问答-------------------- 是不是这个异常:
System.InvalidOperationException
Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.
?
WPF,System.Windows.Controls(not Forms).ListBox/ListView?
是的话,就往下看(这里我用ListView, ListBox应该也差不多)。假定ListView里每一行对应一个MyItem,MyCollection 定义如下:
public class MyCollection: ObservableCollection<MyItem>
如果想remove,像这样(myListView 是一个ListView对象):
ICollectionView view = (ICollectionView)CollectionViewSource.GetDefaultView(myListView.ItemsSource);
MyCollection items = (MyCollection)view.SourceCollection;
items.RemoveAt(...); // 或者Remove() ... --------------------编程问答-------------------- 这两个帖子好象重复了:
http://topic.csdn.net/u/20070823/15/7f701696-5191-4260-8505-fc6dff2f53b5.html?seed=1311380912 --------------------编程问答-------------------- RemoveAt --------------------编程问答-------------------- if (listView1.Items.Count == 0)
{
MessageBox.Show("未发现!", "提示");
return;
}
else
{
if (listView1.SelectedItems.Count == 0)
{
MessageBox.Show("Please select!", "Attention");
return;
}
}
if (listView1.SelectedItems.Count != 0)
{
foreach (ListViewItem lv in listView1.SelectedItems)
{
this.listView1.Items.Remove(lv);
}
}
补充:.NET技术 , C#