private void HandleInput(RoutedEventArgs e) { if (beginSelect) { List <object> itemsToSelect = new List <object>(); List <object> itemsToUnSelect = new List <object>(); // if the mouse position or the start point is outside the window, we trim it inside Point currentPoint = TrimPointToVisibleArea(GetMousePositionRelativeToContent()); Point trimmedStartPoint = TrimPointToVisibleArea(selectPosition.Value); Rect selectionRect = new Rect(currentPoint, trimmedStartPoint); border.UpdatePosition(selectionRect); if (!SelectionMultiple.IsControlKeyDown) { foreach (var item in TreeView.SelectedItems) { var treeViewItem = TreeView.GetTreeViewItemFor(item); Rect itemRect = GetPositionOf(treeViewItem); if (!selectionRect.IntersectsWith(itemRect)) { itemsToUnSelect.Add(item); } } } foreach (var item in items) { if (!item.IsVisible || item.IsEditing) { continue; } Rect itemRect = GetPositionOf(item); if (selectionRect.IntersectsWith(itemRect)) { if (!TreeView.SelectedItems.Contains(item.DataContext)) { itemsToSelect.Add(item.DataContext); } } else { if (!SelectionMultiple.IsControlKeyDown && TreeView.SelectedItems.Contains(item.DataContext)) { itemsToUnSelect.Add(item.DataContext); } } } ((SelectionMultiple)TreeView.Selection).SelectByRectangle(itemsToSelect, itemsToUnSelect); } }
private void OnMouseMove(object sender, MouseEventArgs e) { if (mouseDown) { if (isFirstMove) { isFirstMove = false; border = new BorderSelectionAdorner(content); if (!SelectionMultiple.IsControlKeyDown) { content.ClearSelectionByRectangle(); } } Point currentPoint = Mouse.GetPosition(content); double width = currentPoint.X - startPoint.X; double height = currentPoint.Y - startPoint.Y; double left = startPoint.X; double top = startPoint.Y; double x = startPoint.X < currentPoint.X ? startPoint.X : currentPoint.X; double y = startPoint.Y < currentPoint.Y ? startPoint.Y : currentPoint.Y; // System.Diagnostics.Debug.WriteLine(string.Format("Drawing: {0};{1};{2};{3}",startPoint.X,startPoint.Y,width,height)); if (width < 0) { width = Math.Abs(width); left = startPoint.X - width; } if (height < 0) { height = Math.Abs(height); top = startPoint.Y - height; } border.Width = width; border.Height = height; border.UpdatePosition(new Point(x, y)); double right = left + width; double bottom = top + height; // System.Diagnostics.Debug.WriteLine(string.Format("left:{1};right:{2};top:{3};bottom:{4}", null, left, right, top, bottom)); foreach (var item in items) { if (!item.IsVisible) { continue; } FrameworkElement itemContent = (FrameworkElement)item.Template.FindName("headerBorder", item); Point p = itemContent.TransformToAncestor(content).Transform(new Point()); double itemLeft = p.X; double itemRight = p.X + itemContent.ActualWidth; double itemTop = p.Y; double itemBottom = p.Y + itemContent.ActualHeight; // System.Diagnostics.Debug.WriteLine(string.Format("element:{0};itemleft:{1};itemright:{2};itemtop:{3};itembottom:{4}",item.DataContext,itemLeft,itemRight,itemTop,itemBottom)); if (!(itemLeft > right || itemRight < left || itemTop > bottom || itemBottom < top)) { if (!content.SelectedItems.Contains(item.DataContext)) { ((SelectionMultiple)content.Selection).SelectByRectangle(item); } // System.Diagnostics.Debug.WriteLine("Is selected: " + item); } else { if (!SelectionMultiple.IsControlKeyDown) { ((SelectionMultiple)content.Selection).UnSelectByRectangle(item); } } } e.Handled = true; } }
private void HandleInput(RoutedEventArgs e) { if (mouseDown) { if (Mouse.LeftButton == MouseButtonState.Released) { mouseDown = false; if (border != null) { border.Visibility = Visibility.Collapsed; border.Dispose(); } return; } if (startPoint == GetMousePositionRelativeToContent()) { return; } List <object> itemsToSelect = new List <object>(); List <object> itemsToUnSelect = new List <object>(); // if the mouse position or the start point is outside the window, we trim it inside Point currentPoint = TrimPointToVisibleArea(GetMousePositionRelativeToContent()); Point trimmedStartPoint = TrimPointToVisibleArea(startPoint); if (isFirstMove) { isFirstMove = false; border = new BorderSelectionAdorner(TreeView); } Rect selectionRect = new Rect(currentPoint, trimmedStartPoint); border.UpdatePosition(selectionRect); if (isFirstMove) { if (!SelectionMultiple.IsControlKeyDown) { foreach (var item in TreeView.SelectedItems) { var treeViewItem = TreeView.GetTreeViewItemFor(item); Rect itemRect = GetPositionOf(treeViewItem); if (!selectionRect.IntersectsWith(itemRect)) { itemsToUnSelect.Add(item); } } } } foreach (var item in items) { if (!item.IsVisible || item.IsEditing) { continue; } Rect itemRect = GetPositionOf(item); if (selectionRect.IntersectsWith(itemRect)) { if (isFirstMove) { itemsToSelect.Add(item.DataContext); } else { if (!TreeView.SelectedItems.Contains(item.DataContext)) { itemsToSelect.Add(item.DataContext); } } } else { if (!SelectionMultiple.IsControlKeyDown && TreeView.SelectedItems.Contains(item.DataContext)) { itemsToUnSelect.Add(item.DataContext); } } } ((SelectionMultiple)TreeView.Selection).SelectByRectangle(itemsToSelect, itemsToUnSelect); e.Handled = true; } }