Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 单击按钮,使只读数据网格的选定行中的多个单元格可编辑_C#_Wpf_Datagrid - Fatal编程技术网

C# 单击按钮,使只读数据网格的选定行中的多个单元格可编辑

C# 单击按钮,使只读数据网格的选定行中的多个单元格可编辑,c#,wpf,datagrid,C#,Wpf,Datagrid,问题:在只读数据网格中,我们如何将多个单元格置于编辑模式下的选定行中 在下面的ReadOnlyDataGrid中,btnEdit\u单击事件(如下所示)成功地将所选行的FirstName列单元格置于编辑模式。但在编辑模式下,我需要FirstName和LastName列单元格。如果我将该事件的整个代码包含在for循环中作为for(int I=2;I一次只能将一个单元格置于编辑模式,无论DataGrid的IsReadOnly属性是真还是假 如果要允许用户在单击“编辑”按钮后编辑特定行的单元格,可以参

问题:在
只读
数据网格中,我们如何将多个单元格置于编辑模式下的选定行中


在下面的
ReadOnly
DataGrid中,
btnEdit\u单击
事件(如下所示)成功地将所选行的
FirstName
列单元格置于编辑模式。但在编辑模式下,我需要
FirstName
LastName
列单元格。如果我将该事件的整个代码包含在
for循环中
作为
for(int I=2;I一次只能将一个单元格置于编辑模式,无论DataGrid的
IsReadOnly
属性是真还是假

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

XAML:


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

我注意到
MyDataGrid\u SelectedCellsChanged(…)
事件在
btnEdit\u Click(…)之前调用。
事件。由于当时_editableCustomer为null,因此
if(e.AddedCells.First().Item==\u editableCustomer)
语句在
MyDataGrid\u SelectedCellsChanged(…)
丢失-因此没有单元格处于编辑模式。是的。单击编辑按钮后,将设置\u editableCustomer。它允许用户单击并编辑同一行的其他单元格。关于您关于使用
BeginingEdit
事件的其他建议。在这种情况下,我们如何使除所选行以外的所有行成为
READMONLY
?我的要求是:当用户编辑所选行时,所有其他行必须保持只读模式。
<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 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;
    }