Ejemplo n.º 1
0
        protected virtual void CurrentRendererValueChanged()
        {
            var column = (TreeGrid.Columns[TreeGrid.ResolveToGridVisibleColumnIndex(CurrentCellIndex.ColumnIndex)]);

            TreeGrid.RaiseCurrentCellValueChangedEvent(new TreeGridCurrentCellValueChangedEventArgs(TreeGrid)
            {
                RowColumnIndex = CurrentCellIndex, Record = CurrentCellRendererElement.DataContext, Column = column
            });
            if (column.UpdateTrigger != UpdateSourceTrigger.PropertyChanged || BindingExpression == null)
            {
                return;
            }
            if (BindingExpression.DataItem == null)
            {
                return;
            }
            object oldValue = null;

            if (TreeGrid.View != null)
            {
                oldValue = TreeGrid.View.GetPropertyAccessProvider()
                           .GetValue(BindingExpression.DataItem, BindingExpression.ParentBinding.Path.Path);
            }


            BindingExpression.UpdateSource();
            object Text = null;

            if (TreeGrid.View != null)
            {
                Text = TreeGrid.View.GetPropertyAccessProvider()
                       .GetValue(BindingExpression.DataItem, BindingExpression.ParentBinding.Path.Path);
            }

            object newValue;
            string errorMessage;
            var    treeNode = TreeGrid.View.Nodes.GetNode(BindingExpression.DataItem);

            if (!TreeGrid.ValidationHelper.RaiseCurrentCellValidatingEvent(oldValue, Text, column, out newValue, CurrentCellIndex, CurrentCellElement, out errorMessage, BindingExpression.DataItem, treeNode))
            {
                return;
            }
            if (!ReferenceEquals(newValue, Text))
            {
                SetControlValue(newValue);
            }

            if (TreeGrid.GridValidationMode != GridValidationMode.None)
            {
                TreeGrid.ValidationHelper.ValidateColumn(BindingExpression.DataItem, BindingExpression.ParentBinding.Path.Path, (TreeGridCell)CurrentCellElement, CurrentCellIndex);
            }

            TreeGrid.ValidationHelper.RaiseCurrentCellValidatedEvent(oldValue, Text, column, errorMessage, BindingExpression.DataItem, treeNode);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Let Renderer decide whether the parent grid should be allowed to handle keys and prevent
        /// the key event from being handled by the visual UIElement for this renderer. If this method
        /// returns true the parent grid will handle arrow keys and set the Handled flag in the event
        /// data. Keys that the grid does not handle will be ignored and be routed to the UIElement
        /// for this renderer.
        /// </summary>
        /// <param name="e">A <see cref="KeyEventArgs" /> object.</param>
        /// <returns>
        /// True if the parent grid should be allowed to handle keys; false otherwise.
        /// </returns>
        protected override bool ShouldGridTryToHandleKeyDown(KeyEventArgs e)
        {
            if (!HasCurrentCellState)
            {
                return(false);
            }
            var columnIndex = TreeGrid.ResolveToGridVisibleColumnIndex((this.CurrentCellElement as TreeGridCell).ColumnBase.ColumnIndex);
            var column      = ((TreeGridTemplateColumn)TreeGrid.Columns[columnIndex]);
            var handleTemplatedUIElementKeyDown = FocusManagerHelper.GetWantsKeyInput(column);


            if ((column.hasCellTemplate || column.hasCellTemplateSelector) && (column.hasEditTemplate || column.hasEditTemplateSelector))
            {
                handleTemplatedUIElementKeyDown = false;
            }
            switch (e.Key)
            {
            case Key.Space:
                return(!handleTemplatedUIElementKeyDown);

            case Key.A:
            case Key.V:
            case Key.X:
            case Key.C:
            {
                if (!handleTemplatedUIElementKeyDown && SelectionHelper.CheckControlKeyPressed() && !IsInEditing)
                {
                    return(true);
                }
                break;
            }

            case Key.F2:
            case Key.Escape:
            {
                e.Handled = true;
                return(true);
            }

            case Key.Tab:
                // When press tab continuously  and its reach to last one, then the focus goes to grid.Again if you press upo key, focus any of the content control. to avoid that the condition added.
                if (!IsInEditing && !(TreeGrid.GetLastDataRowIndex() == CurrentCellIndex.RowIndex && TreeGrid.SelectionController.CurrentCellManager.GetLastCellIndex() == CurrentCellIndex.ColumnIndex))
                {
                    // If Column with CellTemplate has editor loaded, then the click on that editor will not as we click or navigate to next cell, we need to remove the focus.
                    base.SetFocus(CurrentCellRendererElement, false);
                }
                return(true);

            case Key.Enter:
            case Key.PageUp:
            case Key.PageDown:
                if (!IsInEditing)
                {
                    // If Column with CellTemplate has editor loaded, then navigation of this key will set the focus.
                    base.SetFocus(CurrentCellRendererElement, true);
                }
                return(true);

            case Key.Home:
            case Key.End:
            case Key.Down:
            case Key.Up:
            case Key.Left:
            case Key.Right:
                // if Column has WantsKeyInput Enabled the navigation should be in editor.
            {
                if (!handleTemplatedUIElementKeyDown && !IsInEditing)
                {
                    base.SetFocus(CurrentCellRendererElement, false);
                    return(true);
                }
                else if (handleTemplatedUIElementKeyDown)
                {
                    return(false);
                }
                else
                {
                    return(!IsInEditing);
                }
            }

            case Key.Delete:
            {
                if (handleTemplatedUIElementKeyDown)
                {
                    return(false);
                }
                return(!IsInEditing);
            }
            }
            return(false);
        }
        /// <summary>
        /// Sets the focus to the specified current cell uielement.
        /// </summary>
        /// <param name="uiElement">
        /// Specifies the corresponding current cell uielement.
        /// </param>
        /// <param name="needToFocus">
        /// Decides whether the focus set to current cell uielement.
        /// </param>
        protected virtual void SetFocus(FrameworkElement uiElement, bool needToFocus)
        {
            if (uiElement != null && IsFocusable)
            {
                UIElement uielement;
                var       focusedElement = FocusManagerHelper.GetFocusedUIElement(CurrentCellRendererElement);
                uielement = focusedElement ?? uiElement;

                TreeGridColumn column = null;
                if (needToFocus)
                {
                    var columnIndex = TreeGrid.ResolveToGridVisibleColumnIndex(CurrentCellIndex.ColumnIndex);
                    column = TreeGrid.Columns[columnIndex];
                }

                //SupportsRenderOptimization condition checked to move the Focus always to CheckBox instead of DataGrid - WPF-22403
                if (needToFocus && (IsInEditing || column.CanFocus() || !SupportsRenderOptimization))
                {
                    if (IsFocused)
                    {
#if WPF
                        if (!uielement.IsFocused)
                        {
                            uielement.Focusable = true;
                            Keyboard.Focus(uielement);
                        }
#endif
                        return;
                    }
#if WPF
                    uielement.Focus();
#else
                    if (uielement is Control)
                    {
                        (uielement as Control).Focus(FocusState.Programmatic);
                        isfocused = false;
                        return;
                    }
#endif
                    isfocused = true;
                }
                else
                {
#if WPF
                    this.TreeGrid.Focus();
#else
                    this.treeGrid.Focus(FocusState.Programmatic);
#endif
                    isfocused = false;
                }
            }
            else
            {
#if WPF
                TreeGrid.Focus();
#else
                TreeGrid.Focus(FocusState.Programmatic);
#endif
                isfocused = false;
            }
        }