Focus CellEditTemplate on selection of Silverlight DataGridRow






3.50/5 (2 votes)
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
.
RowDetailsVisibilityMode="VisibleWhenSelected"
This property specifies that when we select row, its edit template is visible.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 oneEventHanlder
forSelectedCellChanged
.
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.