How to focus a control loaded inside DataTemplate in GridTemplateColumn?
In SfDataGrid, while using the TemplateColumn, when you enter edit mode using keyboard the focus will be given to the content template by default. This is because, as you can load any control within the DataTemplate, it is not possible to focus on the control loaded within the DataTemplate by default. Hence, you need to set focus to the control loaded inside the DataTemplate explicitly. To set the focus to the controls loaded inside the DataTemplate GridTemplateColumn provides an attached property FocusManagerHelper.FocusedElement. You can get the attached property in Syncfusion.UI.Xaml.Grid namespace.
FocusedElement
The attached property FocusedElement gives focus to a particular UIElement loaded inside the DataTemplate. So it is possible to enter edit mode using keyboard itself as the focus will be given to the loaded UIElement. You can also use the attached property inside the CellTemplate.
The following code example shows, how to set the FocusedElement inside DataTemplate
XAML
<syncfusion:GridTemplateColumn MappingName="SalesID"> <syncfusion:GridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding SalesID}"/> </DataTemplate> </syncfusion:GridTemplateColumn.CellTemplate> <syncfusion:GridTemplateColumn.EditTemplate> <DataTemplate> <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <TextBox Text="{Binding SalesID, Mode=TwoWay}" syncfusion:FocusManagerHelper.FocusedElement="True"/> </Grid> </DataTemplate> </syncfusion:GridTemplateColumn.EditTemplate> </syncfusion:GridTemplateColumn>
Sample Links:
Conclusion
I hope you enjoyed learning about how to focus a control loaded inside DataTemplate in GridTemplateColumn.
You can refer to our UWP DataGrid feature tourpage to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our UWP DataGrid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!