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

Keyboard focus within HierarchyChildTemplate

9 Answers 176 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 21 Aug 2013, 02:09 PM
We are using the HierarchyChildTemplate in the GridView to allow interaction with the rows respective record.  When the user enters some text in a TextBox and clicks a button we are persisting their input then we are setting focus back on the TextBox.  We have tried textBox.Focus, FocusManager.SetFocusedElement and Keyboard.Focus.  We also attempted to set e.Handled to true in the Executed hander.  The closest we get is that the TextBox gets logical focus but not keyboard focus.  We are reusing the same control elsewhere within a RadPane and the keyboard focus is set correctly, just not from within the GridView's HierarhcyChildTemplate.  By running Snoop we can se that the window is responding to the keyboard events.

How can we get keyboard focus to remain on the TextBox within the HierarchyChildTempalte?

Thanks

Paul

9 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 26 Aug 2013, 01:05 PM
Hi Paul,

Have you tried to save the instance of the TextBox when loosing the focus and then set it again. For example:

private void Button1_Click(object sender, RoutedEventArgs e)
{
    box1.Focus();
}
 
private void TextBox_LostFocus_1(object sender, RoutedEventArgs e)
{
    box1 = sender as TextBox;
}

Let me know how this works for you.

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Paul
Top achievements
Rank 1
answered on 26 Aug 2013, 02:20 PM
I have tried to call Focus() on the TextBox.  This does work but focus is quickly removed. 

We require the GridView to update in real time to changes posted by other users.  We subscribe to events that we internally raise when state change changes.  When this does we call Reset on the SortDescriptors.  This is where we lose keyboard focus.  How can this be overcome?  We did not see a way to get the grid to resort aside from this mechanism.

Thanks

Paul
0
Dimitrina
Telerik team
answered on 26 Aug 2013, 03:43 PM
Hello Paul,

You cannot do this (keep the keyboard focus) after resetting the SortDescriptors collection as the GridView is recreated then and the instance of the TextBox that you see is different than the one before the Reset.

You can try to find the TextBox (using the ChildrenOfType method) under the expanded row (you can get the row corresponding to the Item, which details you have currently focused, with the GetRowForItem method) and set this instance to be focused.

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Paul
Top achievements
Rank 1
answered on 27 Aug 2013, 03:03 PM
I have tried to use GetRowForItem explicitly passing in radGridViewMain.SelectedItem but this returns null.
if (gridViewMain.SelectedItem != null)
    GridViewRow row = gridViewMain.GetRowForItem(gridViewMain.SelectedItem);

I have also tried the ContainerFromItem using the actual selected cells, which also returns null.
IList<GridViewCellInfo> selectedCells = gridViewMain.SelectedCells;
object o = gridViewMain.ItemContainerGenerator.ContainerFromItem(selectedCells[0].Item);

As a further extreme I tried iteration looking for the selected row. 
var rows = gridViewMain.ChildrenOfType<GridViewRow>();
foreach(GridViewRow row in rows)
{if (row.IsSelected)...

I never find the selected row.  I know this is selected because it was specifically set.  It seems as after the Reset on the SortDescriptors is called that the GridView is in some state where retrieval of the row is being made difficult at best.  If I get this row I can navigate the visual tree and call Focus on the specific element.

Can you provide information as to why we keep getting null?  You stated that the grid is being reconstructed but I would expect that after Reset was called the next execution line in our code would be synchronic to the grid and as such the state would be correct.

Thanks
Paul
0
Yoan
Telerik team
answered on 30 Aug 2013, 03:36 PM
Hello Paul,

I've tried to reproduce the problem you report, but to no avail. I have attached my test project for a reference.

Regards,
Yoan
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Ryan
Top achievements
Rank 1
answered on 04 Sep 2013, 09:28 PM
Yoan,

If you add the bolded line below, your example will accurately depict what our code is doing, and will fail: 

        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            this.clubsGrid.SortDescriptors.Reset();
            var r = this.clubsGrid.GetRowForItem(this.clubsGrid.SelectedItem);
            MessageBox.Show(r.ToString());
        }
0
Ryan
Top achievements
Rank 1
answered on 06 Sep 2013, 01:28 AM
I figured out the issue and a workaround. Apparently, calling SortDescriptors.Reset() is not thread safe. If I call Reset() and immediately after attempt to call the GetRowForItem method, it will always return null, unless I do this workaround of waiting for input idle on the UI thread, which is doing reset. This modified code works. I put the change in bold

   private void Button2_Click(object sender, RoutedEventArgs e)
        {
            this.clubsGrid.SortDescriptors.Reset();
            System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Input, new Action(() =>
            {

                var r = this.clubsGrid.GetRowForItem(this.clubsGrid.SelectedItem);
                MessageBox.Show(r.ToString());
            }));
        }
0
Ryan
Top achievements
Rank 1
answered on 06 Sep 2013, 01:29 AM
Yoan,

Please see my latest post for a workaround for this issue.
0
Dimitrina
Telerik team
answered on 06 Sep 2013, 06:47 AM
Hi,

We are glad to see that you found a solution. Indeed, it takes a little time for the visual elements to be re-created after a Reset operation.

Thank you for sharing it.

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Paul
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Paul
Top achievements
Rank 1
Yoan
Telerik team
Ryan
Top achievements
Rank 1
Share this question
or