Esempio n. 1
0
        /// <summary>
        /// To find the first/last error cell in view if selection is not maintained in datagrid.
        /// </summary>
        private void MoveToErrorCell(SfDataGrid datagrid, bool MoveToFirstCell, ref bool moveToRow)
        {
            RowColumnIndex rowColumnIndex;

            if (MoveToFirstCell)
            {
                rowColumnIndex = new RowColumnIndex(datagrid.GetFirstDataRowIndex(), datagrid.GetFirstColumnIndex());
            }
            else
            {
                rowColumnIndex = new RowColumnIndex(datagrid.GetLastDataRowIndex(), datagrid.GetLastColumnIndex());
            }

            datagrid.SelectionController.MoveCurrentCell(rowColumnIndex);
            var currentRowColumnIndex = datagrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex;
            var gridCellCollections   = GetGridCellCollection(datagrid, currentRowColumnIndex);

            if (gridCellCollections != null)
            {
                foreach (var gridCellItem in gridCellCollections)
                {
                    var gridCell = gridCellItem as GridCell;
                    if (gridCell != null && gridCell.HasError && gridCell.ColumnBase.ColumnIndex == currentRowColumnIndex.ColumnIndex)
                    {
                        datagrid.SelectionController.MoveCurrentCell(new RowColumnIndex(currentRowColumnIndex.RowIndex, gridCell.ColumnBase.ColumnIndex));
                        datagrid.ScrollInView(datagrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex);;
                        moveToRow = false;
                        return;
                    }
                }
            }
        }
        internal void DoActionOnMouseMove(Point pp, GridHeaderCellControl headerCell)
#endif
        {
            bool isLastColumnHidden = false;

#if !WPF
            double hScrollChange = 0;

            for (int i = 0; i < this.dataGrid.VisualContainer.FrozenColumns; i++)
            {
                hScrollChange += this.dataGrid.VisualContainer.ColumnWidths[i];
            }
            // since panel x moves to negative the resizing not properly working, so Abs removed for x calculation WRT-1311, skipping for negative shrinking for first column.
            var pointerPoint = new Point((pp.Position.X - (dataGrid.VisualContainer.HScrollBar.Value - hScrollChange)), Math.Abs(pp.Position.Y - dataGrid.VisualContainer.VScrollBar.Value));
            if (this.isHovering && pp.Properties.IsLeftButtonPressed && this.dragLine != null)
            {
                var  delta = 0.0d;
                int  repeatCount;
                bool isHidden;
                isHidden = isFirstColumnHidden ? dataGrid.VisualContainer.ColumnWidths.GetHidden(dragLine.LineIndex - 1, out repeatCount)
                                               : dataGrid.VisualContainer.ColumnWidths.GetHidden(dragLine.LineIndex + 1, out repeatCount);
                delta = isFirstColumnHidden ? pointerPoint.X
                                            : pointerPoint.X - dragLine.Corner;
                delta = Math.Floor(delta);
#else
            if (this.isHovering && this.dragLine != null && (headerCell.isMouseLeftButtonPressed || headerCell.isTouchPressed))
            {
                //After hiding the column by resizing the column in AddNewRow with edit mode, when pressing enter key rendering issue has been shown in
                //above AddNewRow because the CurrentRowIndex is not updated due the Current column is in Hidden state. Hence the current cell is
                //edited and New row value has been committed.
                CurrentCellEndEdit();
                //Above code raises CurrentCellValidating if customer shows MessageBox then release capture lost and dragline will be null.
                if (dragLine == null)
                {
                    return;
                }
                var  delta = 0.0d;
                int  repeatCount;
                bool isHidden;
                isHidden = isFirstColumnHidden ? dataGrid.VisualContainer.ColumnWidths.GetHidden(dragLine.LineIndex - 1, out repeatCount)
                                               : dataGrid.VisualContainer.ColumnWidths.GetHidden(dragLine.LineIndex + 1, out repeatCount);
                delta = isFirstColumnHidden ? pp.X
                                            : pp.X - dragLine.Corner;
#endif
                double width = Math.Max(0, dragLine.Size + delta);
                var    args  = new ResizingColumnsEventArgs(this.dataGrid)
                {
                    ColumnIndex = dragLine.LineIndex, Width = width
                };
                if (isHidden && dataGrid.AllowResizingHiddenColumns &&
#if !WPF
                    Window.Current.CoreWindow.PointerCursor.Type == UnhideCursor
#else
                    headerCell.Cursor == UnhideCursor
#endif
                    )
                {
                    if (delta <= 0) //WPF-16041 added this condition to sync cursor and action when unhidden last column
                    {
                        isLastColumnHidden = true;
                    }
                    if (delta <= 2 && !isLastColumnHidden)
                    {
                        return;
                    }
                    var hiddenLineIndex = dragLine.LineIndex + 1;
                    if (isFirstColumnHidden)
                    {
                        hiddenLineIndex = 0;
                    }
                    var columnIndex = this.dataGrid.ResolveToGridVisibleColumnIndex(hiddenLineIndex);

                    dataGrid.GridColumnSizer.Suspend();
                    dataGrid.Columns[columnIndex].Width = delta;
                    dataGrid.GridColumnSizer.Resume();
                    if (dataGrid.Columns[columnIndex].IsHidden && delta <= 0)
                    {
                        dataGrid.GridColumnSizer.Suspend();
                        dataGrid.Columns[columnIndex].IsHidden = false;
                        dataGrid.GridColumnSizer.Resume();
                        if (!dataGrid.Columns[columnIndex].IsHidden)
                        {
                            var index = this.dataGrid.ResolveToScrollColumnIndex(this.dataGrid.Columns.IndexOf(dataGrid.Columns[columnIndex]));
                            this.dataGrid.VisualContainer.ColumnWidths.SetHidden(index, index, false);
                            //Need to call ScrollInView to bring the hidden column into view while hiding/unhiding
                            this.dataGrid.VisualContainer.ScrollColumns.ScrollInView(index, -delta);
                        }
                        if (dataGrid.AllowResizingColumns && dataGrid.AllowResizingHiddenColumns)
                        {
                            this.dataGrid.ColumnResizingController.ProcessResizeStateManager(dataGrid.Columns[columnIndex]);
                        }
                        this.dataGrid.VisualContainer.NeedToRefreshColumn = true;
                        this.dataGrid.VisualContainer.InvalidateMeasureInfo();
                        if (this.dataGrid.GridColumnDragDropController != null)
                        {
                            this.dataGrid.GridColumnDragDropController.ColumnHiddenChanged(dataGrid.Columns[columnIndex]);
                        }
                    }
                    else
                    {
                        dataGrid.Columns[columnIndex].IsHidden = false;
                    }
                    this.dataGrid.RowGenerator.UpdateCellStyle(columnIndex);
#if !WPF
                    var cursor = CoreCursorType.Arrow;
                    if (delta >= 0)
                    {
                        dragLine = HitTest(new Point(pointerPoint.X, pointerPoint.Y), out cursor);
                    }
                    else // WPF-16041 Since delta value is negative,need to calculate the dragline through GetVisibleAtLineIndex() for the lastcolumn
                    {
                        dragLine = dataGrid.VisualContainer.ScrollColumns.GetVisibleLineAtLineIndex(hiddenLineIndex);
                        cursor   = ResizingCursor;
                    }
                    if (cursor != CoreCursorType.Arrow)
                    {
                        SetPointerCursor(cursor);
                    }
#else
                    var cursor = headerCell.Cursor;
                    if (dragLine != null && dragLine.LineIndex != hiddenLineIndex)
                    {
                        //dragline will be null when unhiding last column with ColumnSizer - AutoLastColumnFill as it is not inside ViewPort
                        dragLine = dataGrid.VisualContainer.ScrollColumns.GetVisibleLineAtLineIndex(hiddenLineIndex);
                        cursor   = ResizingCursor;
                    }
                    SetPointerCursor(cursor, headerCell);
#endif
                    if (!double.IsNaN(headerCell.Column.MinimumWidth))
                    {
                        delta = Math.Max(delta, headerCell.Column.MinimumWidth);
                    }
                    if (!double.IsNaN(headerCell.Column.MaximumWidth))
                    {
                        delta = Math.Min(delta, headerCell.Column.MaximumWidth);
                    }
                    //WPF-16041 Since delta is negative for last column, change the delta sign and set to Width
                    //delta value switched to positive only when last column is hidden. Other cases, it won't hit
                    if ((dragLine != null) && delta <= 0)
                    {
                        args = new ResizingColumnsEventArgs(this.dataGrid)
                        {
                            ColumnIndex = dragLine.LineIndex, Width = -delta
                        }
                    }
                    ;
                    else if (dragLine != null)
                    {
                        args = new ResizingColumnsEventArgs(this.dataGrid)
                        {
                            ColumnIndex = dragLine.LineIndex, Width = delta
                        }
                    }
                    ;
                }
                if (dragLine != null && !this.dataGrid.RaiseResizingColumnsEvent(args))
                {
                    var headerRow  = dataGrid.RowGenerator.Items.FirstOrDefault(row => row.RowIndex == dataGrid.GetHeaderIndex());
                    var columnBase = headerRow.VisibleColumns.FirstOrDefault(col => col.ColumnIndex == (dragLine.LineIndex));
                    if (columnBase != null && columnBase.ColumnElement is GridHeaderCellControl)
                    {
                        var column = (columnBase.ColumnElement as GridHeaderCellControl).Column;
                        if (dragLine.LineIndex > dataGrid.FrozenColumnCount)
                        {
                            dataGrid.VisualContainer.ScrollColumns.SetLineResize(dragLine.LineIndex, ComputeResizingWidth(column, args.Width != 0 ? args.Width : 1));
                        }
                        else
                        {
                            column.Width = ComputeResizingWidth(column, args.Width != 0 ? args.Width : 1);
                        }
                    }
                    isFirstColumnHidden = false;
                }
            }
            else
            {
                this.isHovering = false;
#if !WPF
                Point point  = pointerPoint;
                var   cursor = ResizingCursor;
#else
                Point point  = pp;
                var   cursor = headerCell.Cursor;
#endif

#if !WPF
                var hit = HitTest(pointerPoint, out cursor);
                if (hit != null && cursor != CoreCursorType.Arrow)
#else
                var hit = HitTest(pp, out cursor);

                //When LineIndex returns the first column index, this condition fails hence the greater than or equal to condition is added.
                if (hit != null && (cursor == UnhideCursor || hit.LineIndex >= dataGrid.GetFirstColumnIndex()))
#endif
                {
                    var lineIndex = hit.LineIndex;

                    if (cursor == UnhideCursor)
                    {
                        lineIndex++;
                        int count;
                        if (this.dataGrid.VisualContainer.ColumnWidths.GetHidden(lineIndex, out count))
                        {
                            lineIndex += count - 1;
                        }
                        else
                        {
                            lineIndex--;
                        }
                    }
                    var index = this.dataGrid.ResolveToGridVisibleColumnIndex(lineIndex);
                    if (index >= 0 && index < this.dataGrid.Columns.Count)
                    {
                        GridColumn column    = this.dataGrid.Columns[index];
                        var        lineindex = this.dataGrid.ResolveToScrollColumnIndex(this.dataGrid.Columns.IndexOf(column));
                        if (!this.CanResizeColumn(column))
                        {
                            return;
                        }
#if WPF
                        this.SetPointerCursor(cursor, headerCell);
#else
                        this.SetPointerCursor(cursor);
#endif
                    }
                }
                bool edgeHit = point.X < 5.0 && point.X > 0;
                if (hit == null && edgeHit)
                {
                    hit = new VisibleLineInfo(0, 0, 0, point.X, 0, true, false);
                }
                if (hit != null || edgeHit)
                {
                    var row = this.dataGrid.VisualContainer.ScrollRows.GetVisibleLineAtPoint(point.Y);
                    if (row != null && row.IsHeader)
                    {
                        this.isHovering = true;
                    }
                }
#if !WPF
                if (!isHovering && Window.Current.CoreWindow.PointerCursor.Type != CoreCursorType.Arrow)
                {
                    SetPointerCursor(CoreCursorType.Arrow);
                }
#else
                if (!isHovering && headerCell.Cursor != Cursors.Arrow && (!headerCell.isMouseLeftButtonPressed || !headerCell.isTouchPressed))
                {
                    SetPointerCursor(Cursors.Arrow, headerCell);
                }
#endif
            }
        }