Articles in this section
Category / Section

How to focus a particular UIElement inside DataTemplate after calling CurrentCell.BeginEdit() or when entering edit mode in WPF Grid?

1 min read

When you use the GridTemplateColumn while entering edit mode, SfDataGrid cannot focus particular UIElement defined inside GridTemplateColumn.EditTemplate. This occurs, as you can load any control within the DataTemplate and it is not possible to focus the control loaded within the DataTemplate by default.

You can set focus to a UIElement inside GridTemplateColumn.EditTemplate by setting FocusManagerHelper.FocusedElement property to true or you can use the FocusManager.FocusedElement to focus the particular UIElement inside DataTemplate.

Consider the following example where you have GridTemplateColumn forCustomerID. When entering into edit mode, TextBox that is defined inside the GridTemplateColumn.EditTemplate is loaded as DataTemplate for the cells of the CustomerID column and TextBox does not get focused. You can set focus to the TextBox when entering edit mode, by setting the attached property “FocusManagerHelper.FocusedElementto Textbox as true.

XAML

<syncfusion:SfDataGrid x:Name="grid"                                      
                                       AllowEditing="True"
                                       AutoGenerateColumns="False"
                                       ItemsSource="{Binding OrderList}"
                                       LiveDataUpdateMode="AllowDataShaping"
                                       ShowRowHeader="True">
 <syncfusion:SfDataGrid.Columns>
 <!--  grid template column is created here  -->
   <syncfusion:GridTemplateColumn HeaderText="Customer ID"   
                                                           MappingName=   "CustomerID">
      <syncfusion:GridTemplateColumn.CellTemplate>
        <DataTemplate>
           <TextBlock FontStyle="Italic"
                                               FontWeight="SemiBold"
                                               Padding="2,0"
                                               Text="{Binding CustomerID}" />
        </DataTemplate>
      </syncfusion:GridTemplateColumn.CellTemplate>
      <syncfusion:GridTemplateColumn.EditTemplate>
        <DataTemplate>
          <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
           <TextBox x:Name="textBox"  FontStyle="Italic" FontWeight="SemiBold"
              syncfusion:FocusManagerHelper.FocusedElement="True"                                
               Text="{Binding CustomerID, Mode=TwoWay}" />
           </Grid>
         </DataTemplate>
       </syncfusion:GridTemplateColumn.EditTemplate>
     </syncfusion:GridTemplateColumn> 
   </syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>

Conclusion

I hope you enjoyed learning about how to focus a particular UIElement inside DataTemplate after calling CurrentCell.BeginEdit() or when entering edit mode in WPF Grid.

You can refer to our WPF Grid feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF Grid documentation 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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied