Datagrid.RowEditEnding不返回更新值 [英] Datagrid.RowEditEnding doesn't return the update value

查看:107
本文介绍了Datagrid.RowEditEnding不返回更新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#WPF的新手,并且正在对DataGrid做一些非常基本的测试。我可以将数据绑定到DataGrid,但是在修改行之后,我只能接收旧数据。有人可以告诉我我的代码有什么问题吗?这是我的代码:

I am new in C# WPF and is doing some very basic test on DataGrid. I can bind the data to the DataGrid but after I amend the row, I can received only old data. Can someone tell me what's wrong in my code? Here is my code:

XAML

<Window x:Class="abc.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid Name="dgEmployee" AutoGenerateColumns="False"
                  RowEditEnding="OnRowEditEnding">
            <DataGrid.Columns>
                <DataGridTextColumn Header="ID" Binding="{Binding EmpNo}"/>
                <DataGridTextColumn Header="Name" Binding="{Binding EmpName}"/>
                <DataGridTextColumn Header="Salary" Binding="{Binding Salary,
                    StringFormat='#,##0.00'}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

代码隐藏

using System.Windows;

namespace abc
{
    public partial class MainWindow : Window
    {
        EmployeeList emp = new EmployeeList();


        public MainWindow()
        {
            InitializeComponent();
            dgEmployee.ItemsSource = emp;
        }

        private void OnRowEditEnding(object sender, System.Windows.Controls.DataGridRowEditEndingEventArgs e)
        {
            var _emp = e.Row.Item as Employee;
            MessageBox.Show(string.Format("updated record:\n{0}\n{1}\n{2}",
            _emp.EmpNo, _emp.EmpName, _emp.Salary));
        }
    }
}

这是Employee类:

and here is the Employee class:

using System.Collections.ObjectModel;

namespace abc
{
    public class Employee
    {
        private int _empNo;
        private string _empName;
        private decimal _salary;

        public Employee(int Id, string Name, decimal wage)
        {
            this._empNo = Id;
            this._empName = Name;
            this._salary = wage;
        }

        public int EmpNo { get { return _empNo; } set { _empNo = value; } }
        public string EmpName { get { return _empName; } set { _empName = value; } }
        public decimal Salary { get { return _salary; } set { _salary = value; } }
    }

    public class EmployeeList : ObservableCollection<Employee>
    {
        public EmployeeList()
            : base()
        {
            Add(new Employee(1, "Bart Simpson", (decimal)10));
            Add(new Employee(2, "Homer Simpson", (decimal)500));
            Add(new Employee(3, "Maggie Simpson", (decimal)200));
        }
    }
}


推荐答案

您需要更改WPF绑定,以在每次更改属性时将更新强制回您的 Employee 类。目前,默认情况下,它仅在OnRowEditEnding事件发生后失去焦点时更新:

You need to change your WPF bindings to force an update back to your Employee class every time the property is changed. At the moment, by default, it only updates when the focus is lost which happens after the OnRowEditEnding event:

<DataGridTextColumn Header="ID" Binding="{Binding EmpNo, UpdateSourceTrigger=PropertyChanged}"/>

这篇关于Datagrid.RowEditEnding不返回更新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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