0

我有一个数据网格,我将一个可观察的集合绑定到它

private void BindDG()
{
    ObservableCollection<Person> personList = new ObservableCollection<Person>(PersonXml.GetPersonList());
    dgPersons.ItemsSource = personList;
}

人员类如下:-

public class Person : INotifyPropertyChanged
{
    public string name { get; set; }
    public string surname { get; set; }

    private int _age;
    public int age
    {
        get { return _age ; }
        set
        {
            if (value != _age)
            {
                _age = value;
                OnPropertyChanged("age");
            }
        }
    }

    private int _salary;
    public int salary
    {
        get { return _salary; }
        set
        {
            if (value != _salary)
            {
                _salary = value;
                OnPropertyChanged("salary");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }   
}

更新方法如下:-

private void Update_Click(object sender, RoutedEventArgs e)
{
    var selPerson = (Person)dgPersons.SelectedItem;
    if (UpdatePerson(selPerson))
        BindDG();
}

我也实现了CellEditEnding事件处理程序无济于事

private void DgPersons_OnCellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    DataGrid grid = (DataGrid)sender;
    grid.CommitEdit(DataGridEditingUnit.Row, true);
}

我究竟做错了什么。当我调试我的应用程序时,年龄和薪水的值永远不会改变,总是和最初开始时一样。

感谢您的帮助和时间

4

1 回答 1

0

尝试使用 BindingList 而不是 ObservableCollection 来聚合项目更改

于 2016-01-20T10:28:37.180 回答