使用DataGrid.BeginEdit()/CancelEdit()/CommitEdit() 方法。
有一些关于编辑的事件需要处理:BeginningEdit、CellEditEnding、PreparingCellForEdit。
使用DataGridCell.IsEditing 属性打开/关闭编辑模式。
您可以获得DataGridRow,您可以从中循环访问它的DataGridCells。这方面有很多教程。
满足您特定需求的准确方法:
1.为所有列创建2个模板。
并将CellTemplate 更改为CellEditingTemplate 以获取可编辑列。
-
在取消/提交后再次将CellTemplate 更改为旧的CellTemplate。
<DataGrid x:Name="DGrid" SelectionUnit="FullRow" AutoGenerateColumns="False" ItemsSource="{Binding Students}" Height="400" CanUserAddRows="False" Margin="10,10,405,18">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Width="100">
<Button Content="Edit" Click="Button_Click_1"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="100">
<Button Content="Cancel" Click="Button_Click_2"/>
<Button Content="Commit" Click="Button_Click_3"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Background="Aquamarine" Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
代码隐藏
// Edit
private void Button_Click_1(object sender, RoutedEventArgs e)
{
DataGridRow row = (DataGridRow)DGrid.ItemContainerGenerator.ContainerFromItem(DGrid.CurrentItem);
_showCellsEditingTemplate(row);
}
// Cancel
private void Button_Click_2(object sender, RoutedEventArgs e)
{
DataGridRow row = (DataGridRow)DGrid.ItemContainerGenerator.ContainerFromItem(DGrid.CurrentItem);
_showCellsNormalTemplate(row);
}
// Commit
private void Button_Click_3(object sender, RoutedEventArgs e)
{
DataGridRow row = (DataGridRow)DGrid.ItemContainerGenerator.ContainerFromItem(DGrid.CurrentItem);
_showCellsNormalTemplate(row, true);
}
private void _showCellsEditingTemplate(DataGridRow row)
{
foreach (DataGridColumn col in DGrid.Columns)
{
DependencyObject parent = VisualTreeHelper.GetParent(col.GetCellContent(row));
while (parent.GetType().Name != "DataGridCell")
parent = VisualTreeHelper.GetParent(parent);
DataGridCell cell = ((DataGridCell)parent);
DataGridTemplateColumn c = (DataGridTemplateColumn)col;
if(c.CellEditingTemplate !=null)
cell.Content = ((DataGridTemplateColumn)col).CellEditingTemplate.LoadContent();
}
}
private void _showCellsNormalTemplate(DataGridRow row, bool canCommit = false)
{
foreach (DataGridColumn col in DGrid.Columns)
{
DependencyObject parent = VisualTreeHelper.GetParent(col.GetCellContent(row));
while (parent.GetType().Name != "DataGridCell")
parent = VisualTreeHelper.GetParent(parent);
DataGridCell cell = ((DataGridCell)parent);
DataGridTemplateColumn c = (DataGridTemplateColumn)col;
if (col.DisplayIndex != 0)
{
if (canCommit == true)
((TextBox)cell.Content).GetBindingExpression(TextBox.TextProperty).UpdateSource();
else
((TextBox)cell.Content).GetBindingExpression(TextBox.TextProperty).UpdateTarget();
}
cell.Content = c.CellTemplate.LoadContent();
}
}
public class ViewModel
{
ObservableCollection<Student> _students = new ObservableCollection<Student>();
public ObservableCollection<Student> Students
{ get { return _students; } set { _students = value; } }
public ViewModel()
{
Students.Add(new Student() { Name = "Prashant", Address = "123, N2 B, Barkheda" });
Students.Add(new Student() { Name = "Amit", Address = "123, N2 B, Piplani" });
Students.Add(new Student() { Name = "Gopi", Address = "Subhash Nagar" });
Students.Add(new Student() { Name = "S. Sachin", Address = "HabibGanj" });
}
}
public class Student
{
public string Name { get; set; }
public string Address { get; set; }
}