绑定到ObservableCollection的WFP DataGrid ItemsSource不会更新超出第一次设置?

Gre*_*reg 1 .net c# wpf observablecollection

我通过DataGrid的"ItemSource"将WPF应用程序DataGrid绑定到ObservableCollection.最初DataGrid确实提出了标题和值,但是对ObservableCollection的升级没有反映出来?(即当我以编程方式返回并增加"Total"值时)我正在使用的ObservableCollection如下.

任何想法为什么以及如何使网格动态更新/绑定正确?

public class SummaryItem
{
    public string ProcessName { get; set; }
    public long Total { get; set; }
    public long Average { get; set; }

    public static SummaryItem ObservableCollectionSearch(ObservableCollection<SummaryItem> oc, string procName)
    {
        foreach (var summaryItem in oc)
        {
            if (summaryItem.ProcessName == procName) return summaryItem;
        }
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑 - 或许一个附加问题是,在这种情况下,DataGrid是否不是我应该使用的控件来可视化什么是有效的内存表?也就是说,SummaryItem的observableCollection实际上是内存表.

Jen*_*lte 7

如果我看对了你就使用了ObservableCollection.如果向ObservableCollection添加项目,则这些更改应始终由WPF反映,但如果您编辑项目的属性(即更改SummaryItem的"Total"值),则不会更改ObservableCollection而是更改为SummaryItem.

为了实现所需的行为,您的SummaryItem必须实现INotifyPropertyChanged接口,以便在更改属性时"通知"WPF:

// implement the interface
public event PropertyChangedEventHandler PropertyChanged;

// use this for every property
private long _Total;
public long Total {
    get {
        return _Total;
    }
    set {
        _Total = value;
        if(PropertyChanged != null) {
            // notifies wpf about the property change
            PropertyChanged(this, new PropertyChangedEventArgs("Total"));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)