This is a migrated thread and some comments may be shown as answers.

Row Keyboard Focus

8 Answers 369 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Nick Anderson
Top achievements
Rank 1
Nick Anderson asked on 14 Jun 2010, 05:46 PM
How do I programmatically focus to the first (or any) row in my RadGridView. I can select the record and scroll to the record programmatically just fine, but it seems as if I can't get keyboard focus on the rows without first clicking into one.

Maybe I'm missing something obvious.

8 Answers, 1 is accepted

Sort by
0
Accepted
Maya
Telerik team
answered on 15 Jun 2010, 01:37 PM
Hello Nick Anderson,

The problem is that the focus is not on the grid. So, in order to enable the  navigation with the keyboard instantly, you need to use the event Loaded:

<telerik:RadGridView Name="playersGrid" Loaded="playersGrid_Loaded"
                                     ItemsSource="{Binding Players}">
 
and set the focus on one of the cells as follows:
private void playersGrid_Loaded(object sender, RoutedEventArgs e)
{
   this.playersGrid.ChildrenOfType<GridViewCell>().First().Focus();
}



Regards,
Maya
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Lana
Top achievements
Rank 1
answered on 12 Jul 2010, 05:19 PM
I tried to implement this solution, however, there are no GridViewCells in the grid when this even fires.  If I try to use this code later, it works just not in the Loaded event.  Any suggestions?  Thanks!
0
Maya
Telerik team
answered on 13 Jul 2010, 02:20 PM
Hi Lana,

I am sending you a sample project illustrating the implementation of the proposed solution. In case it does not meet your exact requirements, please be more specific and provide us with details about your settings and the scenario you want to achieve with the keyboard navigation.
 

Best wishes,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Alisha
Top achievements
Rank 1
answered on 13 Aug 2010, 04:29 PM
Hi,

Im using GridView version 2010.2.714.1040 (runtime version: v2.0.507.27)

I cant find the .ChildrenOfType property on the GridView.

Is there another way to do this now?
0
Yavor Georgiev
Telerik team
answered on 13 Aug 2010, 04:32 PM
Hello Mark Pearl,

 ChildrenOfType is an extension method, which is defined in the Telerik.Windows.Control namespace. Please make sure your code file references this namespace.

Greetings,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Jeff
Top achievements
Rank 1
answered on 19 Nov 2012, 08:13 PM
Maya,

I tried out your demo to try to solve the problem that I'm having getting initial focus and editing state working correctly in an application and I think I've nailed it down to timing. In your example your setting the ViewModel through binding within XAML which means that the ViewModel is fully instantiated and ready once the View attempts to bind to it. In my case I have either the whole ViewModel or at least the collection within it that is loaded asynchronously which means that my RadGridView control's Loaded event fires we do not yet have any GridViewRow items in the grid.

In order to get initial focus working correctly with asynchronous loading of data I'll need some type of event that lets us know when the RadGridView has prepared the corresponding GridViewRows so that I can call methods like RadGridView.ChildrenOfType<GridViewCell>() or RadGridView.ItemContainerGenerator.ContainerFromItem() so that I can set focus and also call BeginEdit on the necessary GridViewCell. Is there any such event available? I've tried Loaded, DataContextChanged, DataLoaded, and LayoutUpdated but none of these worked as required. I'm assuming I'm missing something trivial?

To reproduce my case just remove the DataContext binding to the MyViewModel static resource in the example provided and instead set a timer in the code behind to delay the setting of the DataContext directly like below.

private System.Windows.Threading.DispatcherTimer _timer;
 
public MainWindow()
{
    InitializeComponent();
 
    _timer = new System.Windows.Threading.DispatcherTimer();
    _timer.Interval = TimeSpan.FromSeconds(10);
    _timer.Tick += _timer_Tick;
    _timer.Start();
}
 
void _timer_Tick(object sender, EventArgs e)
{
    MessageBox.Show("Setting the DataContext");
    DataContext = new MyViewModel();
    _timer.Stop();
}
0
Nedyalko Nikolov
Telerik team
answered on 20 Nov 2012, 03:53 PM
Hello Jeff,

You could try "radGridView.RowLoaded" event. You should unhook from this event at the first time, since this event is raised for every single row. Your code should look similar to the following one:

this.radGridView.RowLoaded += this.radGridView_RowLoaded;
        }
 
        void radGridView_RowLoaded(object sender, RowLoadedEventArgs e)
        {
            this.radGridView.RowLoaded -= this.radGridView_RowLoaded;
            // focus code here
        }


Kind regards,
Nedyalko Nikolov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Nebojsa Mancic
Top achievements
Rank 2
answered on 26 Jan 2018, 08:19 PM
This code snippet have resolved my similar issue.
Tags
GridView
Asked by
Nick Anderson
Top achievements
Rank 1
Answers by
Maya
Telerik team
Lana
Top achievements
Rank 1
Alisha
Top achievements
Rank 1
Yavor Georgiev
Telerik team
Jeff
Top achievements
Rank 1
Nedyalko Nikolov
Telerik team
Nebojsa Mancic
Top achievements
Rank 2
Share this question
or