Reputation: 1403
I have a wpf c# application, that loads tasks to a treeView from a text file, the data about the tasks are loading into a list, Im trying to delete data in position I in the list, but i can not figure out how. I have this for loop checking to see if the selected treeView item is equal to the item in position I in the list and if so I wanna delete that item from the list. Here is the for loop which works, im just wondering how to do the actual delete, I've tried things such as .delete
and .remove
which I found on msdna.
for (int i = 0; i < name.Count; ++i)
{
string selectName = ((TreeViewItem)(treeView1.SelectedItem)).Header.ToString();
if (selectName == name[i])
{
//name.Remove(i) or name.Remove[i] or name[i].Remove
}
}
Upvotes: 2
Views: 4812
Reputation: 3688
If name is a List<T>
then you probably want name.RemoveAt(i)
if I understand the question correctly.
Alternatively, you could just use name.RemoveAll(n => n == selectName);
instead of the for loop.
Upvotes: 5
Reputation: 160902
How about:
string selectName = ((TreeViewItem)(treeView1.SelectedItem)).Header.ToString();
name.RemoveAll(x => x == selectedName);
Upvotes: 1
Reputation: 5411
Depending on your application needs, your data structure should have some time of Parent-Child relationship. All you should have to do is remove the Child
that is selected from the parent, and the TreeView
in WPF
should just automatically update.
I suggest you read Josh Smith's article on using ViewModels
with the TreeView
for easier management. Trust me it will make your life a lot easier the sooner you start using a stronger data structure, and just remember that a TreeView
only represents the data provided, and you should have other methods to edit the data directly and not the TreeView
.
Upvotes: 0
Reputation: 916
You have to give List.Remove() an object, not an index.
So, assuming name is a List,
name.Remove((TreeViewItem)treeView1.SelectedItem);
Upvotes: 0