Remove SelectedItem from TreeView

Gabriel picture Gabriel · Dec 20, 2011 · Viewed 12.9k times · Source

Is there a simple way to set a TreeView's SelectedItem to null or equivalent? Also, I need to do this in C# and not in XAML.

Best regards,

Gabriel

Answer

Karl picture Karl · Dec 20, 2011

Not sure what you mean

If you want to remove the item, use this:

treeView1.Items.Remove(treeView1.SelectedItem);

If you want to remove the selection from the treeview, use this:

((TreeViewItem)treeView1.SelectedItem).IsSelected = false;