Tree view right click menu

Jul 19, 2011 at 10:35 AM

I have added a right click menu to my tree view control using xaml. The question is, how do I access a particular selected item that has been right clicked and remove it from the tree?

 

		<my:PopupMenu Name="RCM" RightClickElements="treeView" LeftClickElements="treeView" AccessShortcut="ctrl+alt+m">
			<ListBox>
                <my:PopupMenuItem Header="Add" ImageSource="images/add.jpg"></my:PopupMenuItem>
                <my:PopupMenuItem Header="Delete" ImageSource="images/delete.png"></my:PopupMenuItem>
                <my:PopupMenuSeparator></my:PopupMenuSeparator>
                <my:PopupMenuItem Header="Refresh"></my:PopupMenuItem>
			</ListBox>
		</my:PopupMenu>

 

Thanks.

Jul 19, 2011 at 3:02 PM
        void Menu_RemoveItem(object sender, RoutedEventArgs e)
        {

            TreeViewItem item = RCM.GetClickedElement<TreeViewItem>() as TreeViewItem;
            item.Items.Remove(item);

The following event it being called but fails to remove an item from the tree.

 

Coordinator
Jul 20, 2011 at 1:28 AM
Edited Jul 20, 2011 at 1:35 AM

Are you binding your Treeview to a datasource or collection?

If this is the case remove the item directly from the underlying data instead and your treeview will then automatically follow. If you are not using an observable collection then you might need to reload the Treeview to see the difference.

Ziad