单击按钮,使多个单元格在ReadOnly DataGrid的选定行中可编辑 [英] On a Button Click, making multiple cells editable in a selected row of a ReadOnly DataGrid

查看:56
本文介绍了单击按钮,使多个单元格在ReadOnly DataGrid的选定行中可编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:在 ReadOnly DataGrid中,我们如何在编辑模式下将多个单元格放置在选定的行中?

Question: In a ReadOnly DataGrid, how can we place multiple cells, in a selected row, in Edit mode?

在下面的 ReadOnly DataGrid中, btnEdit_Click 事件(如下所示)成功地将所选行的 FirstName 列单元格置于编辑模式.但是在编辑模式下,我需要 FirstName LastName 列单元格.如果我将事件的整个代码包含在 for循环中,作为 for(int i = 2; i< = 3; i ++){...} 最后一个单元格( LastName )列将获得编辑模式.

In following ReadOnly DataGrid, the btnEdit_Click event (shown below) successfully places the FirstName column cell of selected row in the edit mode. But I need both FirstName and LastName column cells in the edit mode. If I enclose the entire code of that event in a for loop as for (int i=2; i <=3; i++){...} only the last cell (LastName) column gets the edit mode.

备注:

  1. 当用户编辑所选行时,所有其他行都必须保持为 ReadOnly 模式.
  2. 我正在将实体框架核心用于数据库操作(例如,MyDataGrid.ItemsSource = db.Customers.ToList();等)和 .NET Core 3.1 VS2019 最新版本.
  3. 不涉及 MVVM .
  4. 是一个类似的帖子,只是针对一列(不适用于行).
  5. 为简便起见,仅显示两列.实际方案有两列以上.
  1. All other rows have to stay in ReadOnly mode when user is editing the selected row.
  2. I'm using Entity Framework Core for db operations (e.g. MyDataGrid.ItemsSource = db.Customers.ToList(); etc) and .NET Core 3.1, VS2019 latest version.
  3. No MVVM is involved.
  4. This is a similar post but for a column (not for row).
  5. For brevity, only two columns are shown. Real scenario has more than two columns.

DataGrid :

<DataGrid x:Name="MyDataGrid" IsReadOnly="True" SelectionMode="Single" AutoGenerateColumns="False" Margin="0,25,0,0">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Edit">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button x:Name="btnEdit" Content="Edit" Click="btnEdit_Click"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn Header="ID" Visibility="Collapsed" Binding="{Binding CustomerId}" />
        <DataGridTextColumn Header="FirstName" Binding="{Binding FirstName}" />
        <DataGridTextColumn Header="LastName" Binding="{Binding LastName}" />
    </DataGrid.Columns>
</DataGrid>

代码:

private void btnEdit_Click(object sender, RoutedEventArgs e)
{
    DataGridCell dataGridCell;
    MyDataGrid.CurrentCell = new DataGridCellInfo((sender as Button).DataContext, MyDataGrid.Columns[2]);
    var cellContent = MyDataGrid.CurrentCell.Column.GetCellContent(MyDataGrid.CurrentCell.Item);
    if (cellContent != null)
    {
        dataGridCell = (DataGridCell)cellContent.Parent;
        dataGridCell.IsEditing = true;
    }
}

推荐答案

无论DataGrid的 IsReadOnly 属性为True还是False,一次只能将一个单元格置于编辑模式.

You can only place one cell in edit mode at a time no matter the DataGrid's IsReadOnly property is True or False.

如果要允许用户单击编辑"按钮后编辑特定行的单元格,则可以参考以下代码:

If you want to allow users to edit the cells of a specific row after clicking the "Edit" button, you may refer to the below code:

XAML:

<DataGrid x:Name="MyDataGrid" SelectionMode="Single" IsReadOnly="True" AutoGenerateColumns="False" Margin="0,25,0,0"
          SelectedCellsChanged="MyDataGrid_SelectedCellsChanged" SelectionUnit="Cell">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Edit">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button x:Name="btnEdit" Content="Edit" Click="btnEdit_Click"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn Header="ID" Visibility="Collapsed" Binding="{Binding CustomerId}" />
        <DataGridTextColumn Header="FirstName" Binding="{Binding FirstName}" />
        <DataGridTextColumn Header="LastName" Binding="{Binding LastName}" />
    </DataGrid.Columns>
</DataGrid>

后面的代码:

    private Customer _editableCustomer;
    private void btnEdit_Click(object sender, RoutedEventArgs e)
    {
        _editableCustomer = (Customer)MyDataGrid.SelectedCells.First().Item;
    }

    private void MyDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
    {
        if (e.AddedCells.First().Item == _editableCustomer)
            GetDataGridCell(MyDataGrid.SelectedCells.First()).IsEditing = true;
    }

    public static DataGridCell GetDataGridCell(DataGridCellInfo cellInfo)
    {
        var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
        if (cellContent != null)
            return (DataGridCell)cellContent.Parent;

        return null;
    }

但是,我个人不会这样做.我将DataGrid的 IsReadOnly 属性设置为False,并为DataGrid的 BeginningEdit 事件添加事件处理程序.然后,在用户编辑行之前,我可以在事件处理程序中做任何我想做的事情.

However, I personally would not do this. I would set the DataGrid's IsReadOnly property to False and add an event handler for DataGrid's BeginningEdit event. Then I could do whatever I want in the event handler before the user edits the rows.

这篇关于单击按钮,使多个单元格在ReadOnly DataGrid的选定行中可编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆