I'm trying to enable users to move TreeViewItems
around a TreeView
(specifically, by holding control and pressing arrow keys). I can move nodes into and out of other nodes, and I can move nodes up and down on the top level, but when I try to move nodes up within a subnode, it does nothing, and if I try to move nodes down within a subnode, I get the following exception:
Element already has a logical parent. It must be detached from the old parent before it is attached to a new one.
which occurs when I attempt to add the node back to its original collection (after removing it, of course). Here's the original implementation:
private TreeViewItem getParent(TreeViewItem item)
{
for (int i=0; i<fragment_tree.Items.Count; ++i)
{
TreeViewItem r = getParent((TreeViewItem)(fragment_tree.Items[i]), item);
if (r != null)
{
return r;
}
}
return null;
}
private TreeViewItem getParent(TreeViewItem test, TreeViewItem item)
{
for (int i=0; i<test.Items.Count; ++i)
{
if (test.Items[i] == item)
{
return test;
}
}
for (int i=0; i<test.Items.Count; ++i)
{
TreeViewItem r = getParent((TreeViewItem)(test.Items[i]), item);
if (r != null)
{
return r;
}
}
return null;
}
private ItemCollection getContainingList(TreeViewItem item, out int id)
{
return getContainingList(fragment_tree.Items, item, out id);
}
private ItemCollection getContainingList(ItemCollection test, TreeViewItem item, out int id)
{
for (int i=0; i<test.Count; ++i)
{
if (test[i] == item)
{
id = i;
return test;
}
}
for (int i=0; i<test.Count; ++i)
{
ItemCollection r = getContainingList((TreeViewItem)(test[i]), out id);
if (r != null)
{
return r;
}
}
id = -1;
return null;
}
private void fragment_tree_PreviewKeyDown(object sender, KeyEventArgs e)
{
TreeViewItem selected_item = (TreeViewItem)(fragment_tree.SelectedItem);
if (selected_item.Header is String)
{
if (e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl))
{
if (e.Key == Key.Up)
{
int id;
ItemCollection collection = getContainingList(selected_item, out id);
if (collection != null) // it'll never be null, but w/e
{
if (id > 0)
{
collection.RemoveAt(id);
collection.Insert(id-1, selected_item);
selected_item.IsSelected = true;
}
}
e.Handled = true;
}
else if (e.Key == Key.Down)
{
int id;
ItemCollection collection = getContainingList(selected_item, out id);
if (collection != null) // it'll never be null, but w/e
{
if (id < collection.Count)
{
collection.RemoveAt(id);
collection.Insert(id+1, selected_item); // here is the exception
selected_item.IsSelected = true;
}
}
e.Handled = true;
}
else if (e.Key == Key.Left)
{
TreeViewItem parent = getParent(selected_item);
if (parent != null)
{
int id;
ItemCollection collection = getContainingList(parent, out id);
parent.Items.RemoveAt(id);
collection.Insert(id, selected_item);
selected_item.IsSelected = true;
}
e.Handled = true;
}
else if (e.Key == Key.Right)
{
int id;
ItemCollection collection = getContainingList(selected_item, out id);
if (id+1 < collection.Count)
{
TreeViewItem next_item = (TreeViewItem)(collection[id+1]);
collection.RemoveAt(id);
next_item.Items.Insert(0, selected_item);
next_item.IsExpanded = true;
selected_item.IsSelected = true;
}
e.Handled = true;
}
}
}
I tried making a deep clone of the selected TreeViewItem
(though I'd rather not incur the overhead), and got some strange behavior. When I try to move an item up or down within it's subtree, it jumps out to the parent. When I try to move a node on the top level up or down, it deletes its neighbors. I feel as though there's something fundamental I'm missing
private TreeViewItem cloneTreeViewItem(TreeViewItem item)
{
TreeViewItem r = new TreeViewItem();
r.Header = item.Header;
r.Tag = item.Tag;
for (int i=0; i<item.Items.Count; ++i)
{
r.Items.Add(cloneTreeViewItem((TreeViewItem)(item.Items[i])));
}
return r;
}
....
if (e.Key == Key.Up)
{
int id;
ItemCollection collection = getContainingList(selected_item, out id);
if (collection != null) // it'll never be null, but w/e
{
if (id > 0)
{
collection.RemoveAt(id);
TreeViewItem clone = cloneTreeViewItem(selected_item);
collection.Insert(id-1, clone);
clone.IsSelected = true;
}
}
e.Handled = true;
}
else if (e.Key == Key.Down)
{
int id;
ItemCollection collection = getContainingList(selected_item, out id);
if (collection != null) // it'll never be null, but w/e
{
if (id < collection.Count)
{
collection.RemoveAt(id);
TreeViewItem clone = cloneTreeViewItem(selected_item);
collection.Insert(id+1, clone);
clone.IsSelected = true;
}
}
e.Handled = true;
}
.....
I did spend at least an hour researching this topic, I understand that the TreeViewItem
must be detached from its logical parent, and I know from testing that the Parent of a TreeViewItem
is always a TreeView
, I just don't know how to detach it. I know you try to keep the site free of frivolous questions, and I hope mine isn't one. Any insight would be appreciated.
Well thanks for the input, however after further debugging, I realized my issue was the recursive call within getContainingList,
ItemCollection r = getContainingList((TreeViewItem)(test[i]), out id);
should be
ItemCollection r = getContainingList(((TreeViewItem)(test[i])).Items, item, out id);
that's what I get for coding at 2 am