private void SwagItemsControl_Add(object sender, RoutedEventArgs e) { FrameworkElement fe = (FrameworkElement)e.OriginalSource; ICollection <SwagOption> col = (ICollection <SwagOption>)fe.DataContext; SwagOption opt = null; String tag = (fe.Tag ?? "").ToString(); switch (tag) { case "GROUP": opt = new SwagOptionGroup(); break; case "STRING": opt = new StringOption(); break; case "DATE": opt = new DateOption(); break; case "BOOL": opt = new BooleanOption(); break; } col.Add(opt); CollectionViewSource.GetDefaultView(col).Refresh(); }
private void SwagItemsControl_TreeViewItemDrop(object sender, RoutedEventArgs e) { TreeViewItemDropEventArgs tviea = (TreeViewItemDropEventArgs)e; tviea.TargetItem.BorderThickness = new Thickness(0, 0, 0, 0); if (!CheckIfValidDrop(tviea)) //Drops are still possible because TreeViewItemDropPreview can be skipped { return; } MoveType moveType = GetMoveType(tviea); SwagOption targetOpt = (SwagOption)tviea.TargetItem.DataContext; SwagOption droppedOpt = (SwagOption)tviea.DroppedItem.DataContext; SwagOptionGroup originalDroppedParent = (SwagOptionGroup)droppedOpt.Parent; Boolean sameParent = originalDroppedParent == targetOpt.Parent; Int32 originalDroppedSequence = droppedOpt.Sequence, currentDroppedSequence = droppedOpt.Sequence, targetSequence = targetOpt.Sequence, delta = targetSequence - originalDroppedSequence; switch (moveType) { case MoveType.Above: case MoveType.Below: if (!sameParent) { originalDroppedParent.Children.Remove(droppedOpt); droppedOpt.Sequence = -1; //reset sequence targetOpt.Parent.Children.Add(droppedOpt); currentDroppedSequence = droppedOpt.Sequence; delta = targetSequence - currentDroppedSequence; } switch (moveType) { case MoveType.Above: if (delta != 0) { foreach (SwagOption sibling in targetOpt.Parent.Children) { if (sibling.Sequence >= Math.Min(currentDroppedSequence, targetSequence) && sibling.Sequence < Math.Max(currentDroppedSequence, targetSequence)) { sibling.Sequence = (sibling.Sequence + (delta > 0 ? -1 : 1)); } } droppedOpt.Sequence = targetSequence + (delta > 0 ? -1 : 0); if (targetOpt.Parent == null) { CollectionViewSource.GetDefaultView(OptionCollection).Refresh(); } else { CollectionViewSource.GetDefaultView(targetOpt.Parent.Children).Refresh(); } } break; case MoveType.Below: if (delta != 0) { foreach (SwagOption sibling in targetOpt.Parent.Children) { if (sibling.Sequence > Math.Min(currentDroppedSequence, targetSequence) && sibling.Sequence <= Math.Max(currentDroppedSequence, targetSequence)) { sibling.Sequence = (sibling.Sequence + (delta > 0 ? -1 : 1)); } } droppedOpt.Sequence = targetSequence + (delta > 0 ? 0 : 1); if (targetOpt.Parent == null) { CollectionViewSource.GetDefaultView(OptionCollection).Refresh(); } else { CollectionViewSource.GetDefaultView(targetOpt.Parent.Children).Refresh(); } } break; } break; case MoveType.Into: if (tviea.TargetItem.DataContext is SwagOptionGroup grp) { originalDroppedParent.Children.Remove(droppedOpt); droppedOpt.Sequence = -1; //reset sequence grp.Children.Add(droppedOpt); } break; } }