我有一个DataGrid.我的DataGrid的每一行都有Edit Button.当用户单击此编辑按钮时,该行的每个单元格都将进入编辑模式.当我点击任何编辑按钮时,它的模板将更改为保存按钮.当我完成后,我点击该保存按钮,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>
代码隐藏(CS):
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;
}
}
基本上我这里有2个问题:
>当我对一个单元格进行DoubleClick时,该单元格将进入编辑模式.我想停止这种行为,因为我提供了编辑按钮.
>一旦任何行进入编辑模式,如果我更改该行的第一个单元格中的数据,现在如果我移动到下一个单元格,则第一个单元格将其模板更改为cellTemplate.我不想改变它的模板.我的意思是我想让它保留在它的cellEditTemplate中,直到我再次点击Edit Button.
如果有人对任何一个问题都有答案,那么也请发帖.
谢谢.
解决方法:
对于第二个问题,单击“编辑”按钮可以使单元格可编辑,只需将“单元格”模板设置为“文本框”而不是文本块,并在正常情况下将其设置为只读时行可编辑使文本框可编辑为这样即使您移动到下一个单元格,也会保持编辑模式.这也将解决您在编辑模式下进行双击的第一个问题,因为文本框是只读的,直到用户按下编辑按钮,双击效果对他不起作用,因此单元格不会进入编辑模式.