private ITreeItem[] CollectSelectedItems() { List <ITreeItem> resultList = new List <ITreeItem>(); CollectSelectedItemsRecursion(this); return(resultList.ToArray()); void CollectSelectedItemsRecursion(ITreeItem item) { //Collect if (SelectedItemSet.Contains(item)) { resultList.Add(item); } //Recursion if (item is ITreeFolder) { foreach (ITreeItem childItem in ((ITreeFolder)item).ChildItemCollection) { CollectSelectedItemsRecursion(childItem); } } } }
private void ShiftSelectItems(ITreeItem startItem, ITreeItem endItem) { //Unselect SelectedItemSet.UnselectItems(); //Select SelectedItemSet.AddSelectedItem(startItem); ITreeItem[] items = CollectItems(); int startIndex = Array.IndexOf(items, startItem); int endIndex = Array.IndexOf(items, endItem); if (startIndex > endIndex) { int tempValue = endIndex; endIndex = startIndex; startIndex = tempValue; } for (int i = startIndex; i <= endIndex; ++i) { ITreeItem targetItem = items[i]; if (targetItem != startItem) { if (SelectedItemSet.Contains(targetItem)) { SelectedItemSet.RemoveSelectedItem(targetItem); } else { SelectedItemSet.AddSelectedItem(targetItem); } } } }
//Events private void ItemContext_MouseDown(object sender, MouseButtonEventArgs e) { if (e.ChangedButton != MouseButton.Left) { return; } isMousePressed = true; ITreeItem item = null; if (e.OriginalSource is FrameworkElement) { item = GetPressedItem((FrameworkElement)e.OriginalSource); } //Control 혹은 Shift를 누른 상태에선 Pressed에서 이벤트를 종료한다. if (item != null) { if (CanMultiSelect && Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) { //Control select if (SelectedItemSet.Contains(item)) { SelectedItemSet.RemoveSelectedItem(item); } else { SelectedItemSet.AddSelectedItem(item); } pressedItem = SelectedItemSet.Count > 0 ? SelectedItemSet.Last : null; } else if (CanMultiSelect && pressedItem != null && (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))) { //Shift select //pressedItem(Exclusive) ~ Item(Inclusive) 까지 선택한다. ShiftSelectItems(pressedItem, item); } else if (e.ClickCount == 2) { //Double click } else { //Normal select onMouseCapture = true; pressedItem = item; dragStartOffset = (float)e.GetPosition((IInputElement)item).Y; Mouse.Capture((IInputElement)sender, CaptureMode.Element); } } }
private void ItemContext_MouseMove(object sender, MouseEventArgs e) { if (isMousePressed && e.LeftButton != MouseButtonState.Pressed) { ItemContext_MouseUp(sender, null); return; } if (!onMouseCapture) { return; } Point cursorPos = e.GetPosition(ChildItemScrollViewer); Point absoluteCursorPos = e.GetPosition(ChildItemStackPanel); if (!onDragging) { float distance = Mathf.Abs(dragStartOffset - (float)e.GetPosition((IInputElement)pressedItem).Y); if (distance < 10) { return; } else { //Select item if (SelectedItemSet.Contains(pressedItem)) { SelectedItemSet.RemoveSelectedItem(pressedItem); SelectedItemSet.AddSelectedItem(pressedItem); } else { SelectedItemSet.SetSelectedItem(pressedItem); } //Start drag SetDraggingState(true); SetDraggingCursorVisible(true); CreateDraggingClone((FrameworkElement)pressedItem); } } //Dragging GetNodeTarget((float)cursorPos.Y); //Move draggingClone draggingClone.Margin = new Thickness(0d, cursorPos.Y - dragStartOffset, 0d, 0d); draggingClone.Opacity = 0.3d; }