You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让DataGrid新增Person行填写数据后Id单元格变为只读?

问题:DataGrid新增行填写Id后设置为只读

需要在创建新Person行并填写数据后,将DataGrid中的Id单元格设置为只读。尝试通过BeginningEdit事件判断行是否为新行的方案,但仅初始加载的行Id单元格只读,新增并编辑后的行无法实现该效果。

相关代码

Person类

class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string SecondName { get; set; }

    public Person(int id, string firstName, string secondName)
    {
        Id = id;
        FirstName = firstName;
        SecondName = secondName;
    }

    public Person()
    {
        Id = 0;
        FirstName = "";
        SecondName = "";
    }
}

MainWindow.xaml

<Window x:Class="NameDataGrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:NameDataGrid"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <DataGrid x:Name="PersonDataGrid"  AutoGenerateColumns="False" BeginningEdit="PersonDataGrid_BeginningEdit" IsReadOnly="false">
            <DataGrid.Columns>
                <DataGridTextColumn Width="*" Header="Id"  Binding="{Binding Path=Id}"/>
                <DataGridTextColumn Width="*" Header="FirstName"  Binding="{Binding Path=FirstName}"/>
                <DataGridTextColumn Width="*" Header="SecondName"  Binding="{Binding Path=SecondName}"/>
               
            </DataGrid.Columns>

        </DataGrid>
    </Grid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    ObservableCollection<Person> persons;
    public MainWindow()
    {
        InitializeComponent();
        persons = new ObservableCollection<Person>() { 
            new Person(1, "erh", "erh"),
            new Person(2, "erh", "reh"),
            new Person(3, "erh", "erh"),
        };
        PersonDataGrid.ItemsSource = persons;
    }

    private void PersonDataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
    {
        if ((string)e.Column.Header == "Id")
        {
            if (!e.Row.IsNewItem)
            {
                e.Cancel = true;
            }
        }
    }
}
解决方案

原逻辑的问题在于:新行数据提交后,IsNewItem会变为false,但此时如果用户需要再次编辑该行的Id单元格,原逻辑无法区分“已填写Id的新增行”和“初始加载的行”。我们需要通过Person对象的Id值来判断是否允许编辑。

修改BeginningEdit事件处理代码如下:

private void PersonDataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
    if ((string)e.Column.Header == "Id")
    {
        // 获取当前行绑定的Person实例
        if (e.Row.DataContext is Person person)
        {
            // 当Id不为0时,禁止编辑
            e.Cancel = person.Id != 0;
        }
        else
        {
            // 非Person数据时默认禁止编辑
            e.Cancel = true;
        }
    }
}

逻辑说明

  1. 新增行初始状态下Id为0,允许用户编辑填写;
  2. 当用户填写Id并提交数据后,Id值不再为0,后续点击该单元格时会触发BeginningEdit事件,此时person.Id != 0为true,e.Cancel = true会取消编辑操作,实现只读;
  3. 初始加载的行Id不为0,同样会被禁止编辑,满足原有需求。

内容的提问来源于stack exchange,提问作者Ruslan54

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 15:03:14