Focus Lost

2 posts, 0 answers
  1. Brian
    Brian avatar
    14 posts
    Member since:
    Oct 2010

    Posted 05 Jun 2012 Link to this post

    I have a Silverlight page with two RadGridView controls.  The 'parent' grid is SampleGrid and the 'detail' grid is DetailGrid.

    We are trying to provide the ability to use the keyboard to move the selected item in the parent grid while the detail grid is focused.  In other words, while someone is editing the DetailGrid, if they press 'PageDown' or something then the SampleGrid will move to the next row.

    I am using a custom KeyboardCommandProvide to try and make this happen.  The first press of PageDown does cause the SampleGrid to move to the next row, however, the DetailGrid looses focus and pressing PageDown a second time does nothing.  Clicking on the DetailGrid to give it focus then allows one more press of the PageDown functionality.

    How do I keep the focus on the DetailGrid?

    Here is the code to the custom keyboard provider:
    I tried a few variations but none worked.

    private class KeyboardBehavior : DefaultKeyboardCommandProvider
    {
        public KeyboardBehavior(GridViewDataControl grid, GridViewDataControl parentGrid)
            : base(grid)
        {
            Grid = grid;
            ParentGrid = parentGrid;
        }
     
        private GridViewDataControl Grid { get; set; }
        private GridViewDataControl ParentGrid { get; set; }
     
        public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
        {
            if (key == Key.PageDown)
            {
                ParentGrid.Items.MoveCurrentToNext();
                Grid.Items.MoveCurrentToFirst();
                Grid.Focus();
            }
     
            if (key == Key.Enter)
            {
                return new List<ICommand>
                           {
                               RadGridViewCommands.CommitEdit
                           };
            }
     
            return base.ProvideCommandsForKey(key);
        }
    }
  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 06 Jun 2012 Link to this post

    Hi Brian,

    You will need to define two separate keyboard command providers - one for the parent grid and one for those defined in RowDetails. The implementation could look similar to:

    public class ParentGridCustomKeyboardCommandProvider : DefaultKeyboardCommandProvider
        {
            public ParentGridCustomKeyboardCommandProvider(GridViewDataControl grid)
            : base(grid)
            {
                Grid = grid;
            }
      
            private GridViewDataControl Grid{get;set;}
      
            public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
            {
                List<ICommand> commandsToExecute = base.ProvideCommandsForKey(key).ToList();
     
                if (key == Key.PageDown)
                {
                    commandsToExecute.Clear();
                    commandsToExecute.Add(RadGridViewCommands.MoveDown);
                    commandsToExecute.Add(RadGridViewCommands.SelectCurrentUnit);              
                }
                return commandsToExecute;
            }
        }
     
        public class ChildGridCustomKeyboardCommandProvider : DefaultKeyboardCommandProvider
        {
            public ChildGridCustomKeyboardCommandProvider(GridViewDataControl grid)
            : base(grid)
            {
                Grid = grid;
            }
     
            private GridViewDataControl Grid{get;set;}
     
            public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
            {
                List<ICommand> commandsToExecute = base.ProvideCommandsForKey(key).ToList();
     
                if (key == Key.PageDown)
                {
                    commandsToExecute.Clear();
                    var parentGrid = Grid.ParentOfType<GridViewDataControl>();
                    if (parentGrid != null)
                    {
                        parentGrid.Focus();
                        var moveDownCommand = RadGridViewCommands.MoveDown as RoutedUICommand;
                        var selectCurrentItemCommand = RadGridViewCommands.SelectCurrentUnit as RoutedUICommand;
                        moveDownCommand.Execute(null, parentGrid);
                        selectCurrentItemCommand.Execute(null, parentGrid);
                    }
                }
     
                return commandsToExecute;
            }
        }

    I am attaching a sample project that you can use for testing the implementation as well.
    Is that the behavior that you require ? 

    Kind regards,
    Maya
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top