Click here to Skip to main content
13,087,932 members (65,909 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


Posted 14 Dec 2011

Focus CellEditTemplate on selection of Silverlight DataGridRow

, 16 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Edit Selected row Data using CellEditTemplate
Silverlight DataGrid has TemplateColumn to change the appearance of the column.

In general, when we select DataGrid row, we are not able to see CellEditTemplate data.

We need to double time click DataGrid row to see CellEditTemplate.

So then, the scenario is what to do in code to make CellEditTemplate visible on one click.

Let's go through the following steps:

Step 1: Place DataGrid control on the XAML page and add DataGridTemplate column with CellTemplate and CellEditTemplate.

File: EmployeeListControl.xaml

<DataGrid  Name="ItemGrid"
       ItemsSource="{Binding EmployeeList}"
       SelectedItem="{Binding SelectedEmployee, Mode=TwoWay}">
                 Header="Employee Name"
                          <Grid  FocusManager.FocusedElement="{Binding ElementName=EmployeeTextBox}"> 
                               <TextBox Margin=" 4,4,4,4"
                                   Text="{Binding EmployeeName, Mode=TwoWay}"
                                   Cursor="Hand" />
                            <TextBlock Margin=" 4,4,4,4"
                                 Text="{Binding EmployeeName,Mode=OneWay}"
                                 Cursor="Hand" />

In the above code, I have placed one DataGrid into the UserControl, currently data are binding using MVVM through ViewModel, you are aware about Binding Properties to Control.

So we skip ViewModel code here.

In DataGrid, create DataGridTemplateColumn to Edit Employee Name to click on the Row.

In TemplateColumn, create two Templates, one is CellTemplate to view data on grid, and another is CellEditTemplate to edit data on grid.


In CellTemplate, place one TextBlock to display EmployeeName.


In CellEditTemplate, place TextBox to Edit EmployeeName to click on grid row.

To set focus on TextBox, we have to put TextBox into the Grid, and have to set FocusManager.FocusedElement propety to TextBox name. So when we click on DataGrid Row, focus automatically sets to TextBox.

Now, we have to set some properties to DataGrid.

  1. RowDetailsVisibilityMode="VisibleWhenSelected"

    This property specifies that when we select row, its edit template is visible.

  2. SelectionUnit="Cell"

    This property identifies that when you can select row, it will select current focus cell instead of whole row.
    Now, have to create one EventHanlder for SelectedCellChanged.

Step 2: Create one EventHandler for SelectedCellChanged and write logic to set focus on selected cell control.

File: EmployeeListControl.xaml.cs

private void watchlistItemGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
           if (e.AddedCells.Count == 0) return;
           var currentCell = e.AddedCells[0];
           string header = (string)currentCell.Column.Header;

The above code snippet will set focus on selected cell's control and notify grid to edit data.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Hiren Khirsaria
Software Developer (Senior)
India India
has Total 7+ years of Experience in Microsoft.Net Environment.

Major Development Skills are : WPF, Silverlight, Windows Mobile, WCF RIA Service, C#, Sql Server & ASP.Net

Follow him on :

You may also be interested in...

Comments and Discussions

GeneralReason for my vote of 2 RowDetailsVisibilityMode - "This pro... Pin
sagarok14-Dec-11 19:21
membersagarok14-Dec-11 19:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 16 Dec 2011
Article Copyright 2011 by Hiren Khirsaria
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid