Click here to Skip to main content
Click here to Skip to main content

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}"
       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)

About the Author

Hiren Khirsaria
Software Developer (Senior)
India India
has Total 5+ years of Experience in Microsoft.Net Environment. out of that,
 
2 + year of Excperience in .Net Development with C# and SQL Server 2005/2008.
 
3 + years of Experience in WPF/Silverlight.
 
Current area of Development in Mono for Android (Xamarin), Silverlight/ WPF and Windows Phone 7 Application.
 
Follow him on : http://hirenkhirsaria.blogspot.com/

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 16 Dec 2011
Article Copyright 2011 by Hiren Khirsaria
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid