How can we get keyboard focus to remain on the TextBox within the HierarchyChildTempalte?
Thanks
Paul
9 Answers, 1 is accepted
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
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 >>

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
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.
Didie
Telerik
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 >>

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
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
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 >>

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());
}

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());
}));
}

Please see my latest post for a workaround for this issue.
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.
Didie
Telerik
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 >>