简体   繁体   English

如何使用不知道何时添加项目的ItemsSource更新Datagrid?

[英]How to update Datagrid with an ItemsSource that doesn't know when an item is added?

I know I Should implement the INotifyPropertyChanged interface if I want to notify the grid that it should update for the properties in the items, So I did and it was working before. 我知道,如果我INotifyPropertyChanged格它应该针对项目中的属性进行更新,则应该实现INotifyPropertyChanged接口,所以我做到了并且它之前一直在工作。

I decided to change the getter of the bound ObservableCollection so that it would be relative to my DataController class. 我决定更改绑定的ObservableCollection的吸气剂,使其相对于我的DataController类。 This way, the Add() method is never called so in theory, the NotifyCollectionChangedEventHandler is never called. 这样,从不调用Add()方法,因此从理论上讲,从不调用NotifyCollectionChangedEventHandler Does that sound about right? 那个听起来是对的吗?

This is the grid: 这是网格:

绑定网格

De grid is bound to my TeamQualificationMatches collection residing in the ViewModel : De grid绑定到ViewModel驻留的我的TeamQualificationMatches集合:

public ObservableCollection<TeamMatchModel> TeamQualificationMatches
{
    get 
    {
        return new ObservableCollection<TeamMatchModel>(App.Data.GetDoubleTeamMatches().Where(
            match => match.TournamentId == CurrentTournament.Id &&
                     match.Status == MatchStatus.QualificationsPlaying).ToList<TeamMatchModel>());
    }
}

And most of the TeamMatchModel instances implement OnPropertyChanged : 和大多数的TeamMatchModel情况下实现OnPropertyChanged

public string Notes
{
    get { return _teamMatch.Notes; }
    set
    {
        _teamMatch.Notes = value;
        OnPropertyChanged("Notes");
    }
}

My logic used to work when my TeamQualificationMatches were as follows: TeamQualificationMatches如下时,我的逻辑曾经起作用:

private ObservableCollection<TeamMatchModel> _teamQualificationMatches = new ObservableCollection<TeamMatchModel>();
public ObservableCollection<TeamMatchModel> TeamQualificationMatches
{
    get
    {
        return _teamQualificationMatches;
    }
}

When I'm changing the status of a TeamMatchModel instance, I do call OnPropertyChanged("TeamQualificationMatches") . 当我更改TeamMatchModel实例的状态时,确实要调用OnPropertyChanged("TeamQualificationMatches") So I don't know why the grid doesn't reflect on this update. 因此,我不知道为什么网格不反映此更新。

public MatchStatus Status
{
    get { return (MatchStatus)_teamMatch.Status; }
    set 
    {
        _teamMatch.Status = (int)value;
        OnPropertyChanged("Status");
        switch (value)
        {
            case MatchStatus.QualificationsPlaying:
                OnPropertyChanged("TeamQualificationMatches");
                break;
            case MatchStatus.PreselectionsPlaying:
                OnPropertyChanged("TeamPreSelectionMatches");
                break;
            case MatchStatus.RoundAPlaying:
                OnPropertyChanged("TeamWinnerMatches");
                break;
            case MatchStatus.RoundBPlaying:
                OnPropertyChanged("TeamLoserMatches");
                break;
        }
    }
}

It must be something simple that I'm overlooking. 这一定是我所忽略的简单事物。 Any help is appreciated. 任何帮助表示赞赏。

You shouldn't bind the UI to a property in the view model that always gives a new value. 您不应该将UI绑定到视图模型中始终提供新值的属性。 This is why your original version works. 这就是为什么您的原始版本可以工作的原因。 Adding and removing items from the observable collection should update the UI as it implements INotifyCollectionChanged. 在可观察集合中添加和删除项目应该在实现INotifyCollectionChanged时更新UI。 So keep a single observable collection, bind it to the UI and when you need to just clear it and replace the items. 因此,请保留一个可观察的集合,将其绑定到UI,以及在您需要清除它并替换项目时使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM