Ejemplo n.º 1
0
        private void Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Button button = sender as Button;

            if (button?.DataContext == null)
            {
                return;
            }
            GridCell gridCellVM = button.DataContext as GridCell;

            if (gridCellVM == null)
            {
                return;
            }
            GridLayoutViewModel vm = GridLayoutViewModel;

            if (vm == null)
            {
                return;
            }

            if ((Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)) && vm.SelectedCells.Count > 0)
            {
                GridCell lastSelected = vm.SelectedCells[vm.SelectedCells.Count - 1];
                System.Drawing.Rectangle        extent    = System.Drawing.Rectangle.Union(lastSelected.CellExtent, gridCellVM.CellExtent);
                Dictionary <GridCell, GridCell> selectSet = new Dictionary <GridCell, GridCell>();
                foreach (GridCell cell in vm.SelectedCells)
                {
                    selectSet[cell] = cell;
                    extent          = System.Drawing.Rectangle.Union(cell.CellExtent, extent);
                }

                while (true)
                {
                    int selCount = 0;
                    foreach (GridCell cell in vm.GridElements)
                    {
                        if (cell.CellExtent.IntersectsWith(extent))
                        {
                            if (selectSet.ContainsKey(cell))
                            {
                                continue;
                            }
                            extent = System.Drawing.Rectangle.Union(cell.CellExtent, extent);
                            vm.AddToSelection(cell);
                            selectSet[cell] = cell;
                            selCount++;
                        }
                        else
                        {
                            cell.IsHighlighted = false;
                        }
                    }

                    if (selCount == 0)
                    {
                        break;
                    }
                }
            }
            else
            {
                if (!Keyboard.IsKeyDown(Key.LeftCtrl) && !Keyboard.IsKeyDown(Key.RightCtrl))
                {
                    vm.ClearSelection();
                }
                if (vm.SelectedCells.Contains(gridCellVM))
                {
                    gridCellVM.IsSelected    = false;
                    gridCellVM.IsHighlighted = false;
                    vm.RemoveFromSelection(gridCellVM);
                }
                else
                {
                    gridCellVM.IsSelected    = true;
                    gridCellVM.IsHighlighted = true;
                    vm.AddToSelection(gridCellVM);
                }

                if (vm.SelectedCells.Count == 0)
                {
                    return;
                }
            }
        }