private TreeViewItem GetContainerFromStuff(Stuff stuff) { Stack <Stuff> _stack = new Stack <Stuff>(); _stack.Push(stuff); Stuff parent = stuff.Parent; while (parent != null) { _stack.Push(parent); parent = parent.Parent; } ItemsControl container = TheTreeView; while ((_stack.Count > 0) && (container != null)) { Stuff top = _stack.Pop(); container = (ItemsControl)container.ItemContainerGenerator.ContainerFromItem(top); } return(container as TreeViewItem); }
private void TheTreeView_Drop(object sender, DragEventArgs e) { e.Effects = DragDropEffects.None; e.Handled = true; // Verify that this is a valid drop and then store the drop target TreeViewItem container = GetNearestContainer(e.OriginalSource as UIElement); if (container != null) { Stuff sourceStuff = (Stuff)e.Data.GetData(typeof(Stuff)); Stuff targetStuff = (Stuff)container.Header; if ((sourceStuff != null) && (targetStuff != null)) { if (!targetStuff.MoreStuff.Contains(sourceStuff)) { _targetStuff = targetStuff; e.Effects = DragDropEffects.Move; } } } }