wpf Datagrid.RowEditEnding 不返回更新值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16236149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Datagrid.RowEditEnding doesn't return the update value
提问by Richie
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:
我是 C# WPF 新手,正在对 DataGrid 进行一些非常基本的测试。我可以将数据绑定到 DataGrid,但在我修改行后,我只能收到旧数据。有人能告诉我我的代码有什么问题吗?这是我的代码:
XAML
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>
Code-behind
代码隐藏
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));
}
}
}
and here is the Employee class:
这是 Employee 类:
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));
}
}
}
回答by ChrisO
You need to change your WPF bindings to force an update back to your Employeeclass 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:
您需要更改 WPF 绑定以在Employee每次更改属性时强制更新回您的类。目前,默认情况下,它仅在 OnRowEditEnding 事件之后失去焦点时更新:
<DataGridTextColumn Header="ID" Binding="{Binding EmpNo, UpdateSourceTrigger=PropertyChanged}"/>
回答by hackystack
Workaround: update a static list of rows to be changed use another event to update the row(s) after the roweditending event
解决方法:更新要更改的静态行列表,在 roweditending 事件之后使用另一个事件更新行
private void datagrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
rows2change.Add(e.Row.GetIndex());
}
private void datagrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
if (rows2change.Count > 0)
{
saveMyData(rows2change[0]);
rows2change.Remove(rows2change[0]);
}
}

