我有一个DataGrid。我在DataGrid的每一行都有编辑按钮。当用户单击此编辑按钮时,该行的每个单元格都将进入编辑模式。当我点击任何编辑按钮时,它的模板将更改为保存按钮。完成后,我点击保存按钮,DataGridRow退出编辑模式,保存按钮再次更改为编辑按钮。这很好用。
这是我的代码:
Xaml:
<DataGrid Grid.Column="1" Grid.Row="3" AutoGenerateColumns="False" RowHeaderWidth="0" GridLinesVisibility="Vertical"
ItemsSource="{Binding Source={StaticResource ItemsViewSource}}" SelectedItem="{Binding SelectedItem}"
CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False"
SelectionMode="Single" SelectionUnit="FullRow">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Name of the Item" Width="*" SortDirection="Ascending">
<DataGridTemplateColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader" BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
<Setter Property="HorizontalContentAlignment" Value="Left"/>
</Style>
</DataGridTemplateColumn.HeaderStyle>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" PreviewKeyDown="TextBox_PreviewKeyDown"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Left" Margin="5,0,0,0" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Edit" Width="50" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Edit" Style="{StaticResource EditSaveButton}"
Command="{Binding DataContext.EditItemCommand,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}"
Click="EditButton_Click"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Delete" Width="70" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Delete" Style="{StaticResource DeleteButton}"
Command="{Binding DataContext.CancelItemEditOrDeleteCommand,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Page}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
代码隐藏:
void EditButton_Click(object sender, RoutedEventArgs e)
{
int colIndex = 0;
int rowIndex = 0;
DependencyObject dep = (DependencyObject)e.OriginalSource;
while (dep != null && !(dep is DataGridCell))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
DataGridRow row = null;
if (dep is DataGridCell)
{
colIndex = ((DataGridCell)dep).Column.DisplayIndex;
while (dep != null && !(dep is DataGridRow))
{
dep = VisualTreeHelper.GetParent(dep);
}
row = (DataGridRow)dep;
rowIndex = FindRowIndex(row);
}
while (dep != null && !(dep is DataGrid))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
DataGrid dg = (DataGrid)dep;
if (row != null)
{
var rows = GetDataGridRows(dg);
if (row.IsEditing)
{
dg.CommitEdit(DataGridEditingUnit.Row, true);
foreach (DataGridRow r in rows)
{
r.IsEnabled = true;
}
}
else
{
dg.CurrentCell = new DataGridCellInfo(dg.Items[rowIndex], dg.Columns[0]);
dg.BeginEdit();
foreach (DataGridRow r in rows)
{
if (!(r.IsEditing))
{
r.IsEnabled = false;
}
}
}
}
}
public DataGridCell GetDataGridCell(DataGridCellInfo cellInfo)
{
var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
if (cellContent != null)
return (DataGridCell)cellContent.Parent;
return null;
}
private int FindRowIndex(DataGridRow row)
{
DataGrid dataGrid = ItemsControl.ItemsControlFromItemContainer(row) as DataGrid;
int index = dataGrid.ItemContainerGenerator.IndexFromContainer(row);
return index;
}
public IEnumerable<DataGridRow> GetDataGridRows(DataGrid grid)
{
var itemsSource = grid.ItemsSource as IEnumerable;
if (null == itemsSource) yield return null;
foreach (var item in itemsSource)
{
var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
if (null != row) yield return row;
}
}
基本上我有两个问题:
- 双击单元格时,该单元格将进入编辑模式。当我提供编辑按钮时,我想停止这种行为
- 一旦任何一行进入编辑模式,如果我更改该行第一个单元格中的数据,现在如果我移动到下一个单元格,第一个单元格将其模板更改为cellTemplate。我不想更改它的模板。我的意思是,当我再次单击编辑按钮时,我希望它保持在它的cellEditTemplate中
如果有人对其中任何一个问题有答案,也请发帖。
谢谢。
对于您的第二个问题,要使单元格始终可编辑,请单击"编辑"按钮,您可以简单地将"单元格"模板设置为"文本框",而不是文本块,并在正常情况下使其只读。当行可编辑时,使文本框可编辑,这样即使移动到下一个单元格,它也将保持在编辑模式。这也将解决您的第一个问题,即单元格进入编辑模式双击,因为文本框是只读的,直到用户按下"编辑"按钮,双击效果对他不起作用,因此单元格不会进入编辑模式。
只需尝试之类的东西
void Cell_Click(object sender, RoutedEventArgs e){
(...)
e.Handled;
}
因为这将阻止进一步的事件回调。
此外,这个主题可能会有所帮助。