/// <summary>
 /// Set focus to UIElement loaded in DataTemplate of GridCell on loading. since OnEditElementloaded will not fire for GridCell again when start editing.
 /// </summary>
 /// <param name="cellRowColumnIndex"></param>
 /// <param name="uiElement"></param>
 /// <param name="cellRect"></param>
 protected override void OnArrange(RowColumnIndex cellRowColumnIndex, FrameworkElement uiElement, Rect cellRect)
 {
     base.OnArrange(cellRowColumnIndex, uiElement, cellRect);
     if (CanFocus && HasCurrentCellState && IsInEditing && FocusManagerHelper.GetFocusedUIElement(CurrentCellRendererElement) != null)
     {
         SetFocus(true);
         CanFocus = false;
     }
 }
Esempio n. 2
0
        protected virtual void OnEditElementLostFocus(object sender, RoutedEventArgs e)
#endif
        {
            //OnLostKeyboardFocus - Take care of validation in WPF
            if (this.HasCurrentCellState && this.CurrentCellRendererElement == sender)
            {
                this.isfocused = false;
            }

#if UWP
            var uiElement = sender as UIElement;
            if (uiElement != null)
            {
                var IsKeyboardFocusWithin = false;
                var focusedElement        = FocusManager.GetFocusedElement() as DependencyObject;
                if (focusedElement != null)
                {
                    if (focusedElement == sender)
                    {
                        return;
                    }
                    List <DependencyObject> childrens = new List <DependencyObject>();
                    GridUtil.Descendant(uiElement, ref childrens);
                    IsKeyboardFocusWithin = childrens.Contains(focusedElement);
                }
                if (IsKeyboardFocusWithin)
                {
                    return;
                }
            }

            if (HasCurrentCellState && !CheckToAllowFocus(FocusManager.GetFocusedElement(), sender))
            {
                Control element = sender as Control;

                if (FocusManagerHelper.GetFocusedUIElement(this.CurrentCellRendererElement) != null)
                {
                    element = FocusManagerHelper.GetFocusedUIElement(this.CurrentCellRendererElement);
                }

                await element.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    element.Focus(FocusState.Programmatic);
                });
            }
#endif
        }
Esempio n. 3
0
        private void timerTick(object sender, EventArgs e)
        {
            var isBusy = this.BusyIndicator.IsBusy;

            if (!isBusy)
            {
                this.focusedElement       = FocusManagerHelper.GetFocusedElement(this.StackPanel) as Control;
                this.BusyIndicator.IsBusy = true;
            }
            else
            {
                this.BusyIndicator.IsBusy = false;
                if (this.focusedElement != null)
                {
                    this.focusedElement.IsEnabledChanged += focusedElement_IsEnabledChanged;
                }
            }
        }
Esempio n. 4
0
        internal void OnLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            var uiElement = sender as UIElement;

            if (uiElement != null)
            {
                var IsKeyboardFocusWithin = false;
                var focusedElement        = e.NewFocus as DependencyObject;
                if (focusedElement != null)
                {
                    if (focusedElement == e.Source)
                    {
                        return;
                    }
                    IsKeyboardFocusWithin = this.IsKeyboardFocusWithin(uiElement, focusedElement);
                }

                if (IsKeyboardFocusWithin)
                {
                    return;
                }
            }

            //WPF-24276 - Need to ensure the CurrentCellState once again after raising the ValidationEvents.
            if (!CheckToAllowFocus(e.NewFocus, sender) && this.HasCurrentCellState)
            {
                e.Handled = true;
                if (this.CurrentCellElement is TreeGridCell && (this.CurrentCellElement as TreeGridCell).ColumnBase.TreeGridColumn.IsTemplate)
                {
                    if (FocusManagerHelper.GetFocusedUIElement(this.CurrentCellRendererElement) != null)
                    {
                        (FocusManagerHelper.GetFocusedUIElement(this.CurrentCellRendererElement)).CaptureMouse();
                    }
                }
                else
                {
                    (sender as FrameworkElement).CaptureMouse();
                }
            }
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
 /// <summary>
 /// Gets a control that needs to get focus.
 /// </summary>
 /// <param name="uiElement">The FrameworkElement to set focus.</param>
 /// <returns>Returns the FramewrokElement which can get focus.</returns>
 /// <remarks>
 /// GridCellMultiColumnDropDownRenderer returns editor to set focus.
 /// </remarks>
 internal virtual Control GetFocusedUIElement(FrameworkElement uiElement)
 {
     return(FocusManagerHelper.GetFocusedUIElement(CurrentCellRendererElement));
 }
        /// <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;
            }
        }
        /// <summary>
        /// Shoulds the grid try automatic handle key down.
        /// </summary>
        /// <param name="e">The <see cref="KeyEventArgs"/> instance containing the event data.</param>
        /// <returns></returns>
        protected override bool ShouldGridTryToHandleKeyDown(KeyEventArgs e)
        {
            if (!HasCurrentCellState)
            {
                return(true);
            }
            var columnIndex = DataGrid.ResolveToGridVisibleColumnIndex(CurrentCellIndex.ColumnIndex);
            var dataColumn  = (CurrentCellElement as GridCell).ColumnBase;

            var handleTemplatedUIElementKeyDown = FocusManagerHelper.GetWantsKeyInput(dataColumn.GridColumn);

            if ((dataColumn.GridUnBoundRowEventsArgs != null && dataColumn.GridUnBoundRowEventsArgs.EditTemplate != null))
            {
                handleTemplatedUIElementKeyDown = false;
            }

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

            case Key.Tab:
                if (!IsInEditing && !(DataGrid.GetLastDataRowIndex() == CurrentCellIndex.RowIndex && DataGrid.SelectionController.CurrentCellManager.GetLastCellIndex() == CurrentCellIndex.ColumnIndex))
                {
                    base.SetFocus(CurrentCellRendererElement, false);
                }
                return(true);

            case Key.Enter:
            case Key.PageUp:
            case Key.PageDown:
                if (!IsInEditing)
                {
                    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 (!handleTemplatedUIElementKeyDown && !IsInEditing)
                {
                    base.SetFocus(CurrentCellRendererElement, false);
                    return(true);
                }
                else if (handleTemplatedUIElementKeyDown)
                {
                    return(false);
                }
                else
                {
                    return(!IsInEditing);
                }
            }
            }
            return(base.ShouldGridTryToHandleKeyDown(e));
        }