Click here to Skip to main content
15,891,607 members
Articles / Programming Languages / C#
Tip/Trick

Focus CellEditTemplate on selection of Silverlight DataGridRow

Rate me:
Please Sign up or sign in to vote.
3.50/5 (2 votes)
16 Dec 2011CPOL1 min read 24.6K   1
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}"
       AutoGenerateColumns="False"
       HorizontalAlignment="Left"
       SelectedCellsChanged="watchlistItemGrid_SelectedCellsChanged"
       RowDetailsVisibilityMode="VisibleWhenSelected"
       SelectionMode="Extended"
       SelectionUnit="Cell"          
       VerticalAlignment="Top"
       SelectedItem="{Binding SelectedEmployee, Mode=TwoWay}">
          <DataGrid.Columns>
             <DataGridTemplateColumn                                                              
                 Header="Employee Name"
                 IsReadOnly="False"
                 SortMemberPath="EmployeeName">
                    <DataGridTemplateColumn.CellEditingTemplate>
                       <DataTemplate>
                          <Grid  FocusManager.FocusedElement="{Binding ElementName=EmployeeTextBox}"> 
                               <TextBox Margin=" 4,4,4,4"
                                   Width="150"
                                   x:Name="EmployeeTextBox"
                                   VerticalAlignment="Center"
                                   HorizontalAlignment="Left"
                                   Text="{Binding EmployeeName, Mode=TwoWay}"
                                   FontWeight="Bold"
                                   Cursor="Hand" />
                           </Grid>
                        </DataTemplate>
                      </DataGridTemplateColumn.CellEditingTemplate>
                      <DataGridTemplateColumn.CellTemplate>
                         <DataTemplate>
                            <TextBlock Margin=" 4,4,4,4"
                                 Width="150"
                                 VerticalAlignment="Center"
                                 HorizontalAlignment="Left"
                                 Text="{Binding EmployeeName,Mode=OneWay}"
                                 FontWeight="Bold"
                                 Cursor="Hand" />
                         </DataTemplate>
                       </DataGridTemplateColumn.CellTemplate>
               </DataGridTemplateColumn>
         </DataGrid.Columns>
</DataGrid>

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.

CellTemplate

In CellTemplate, place one TextBlock to display EmployeeName.

CellEditTemplate

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

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

License

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


Written By
Team Leader Reputed IT Company
India India
Having 9+ years of experience in Microsoft.Net Technology.
Experience in developing applications on Microsoft .NET Platform ( Asp.Net, WPF, Silverlight, Windows Phone 7/8).
Experience and knowledge of software design methodologies (Agile), object oriented design, and software design patterns (MVVM).
Experience in Developing android mobile application using Xamarin (mono for android) framework.

http://hirenkhirsaria.blogspot.com/

Comments and Discussions

 
GeneralReason for my vote of 2 RowDetailsVisibilityMode - "This pro... Pin
sagarok14-Dec-11 19:21
sagarok14-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.