2015-11-16 28 views
1

我有這樣的代碼的ObservableCollection

這是我PersonModel PersonModel.cs

public class PersonModel 
{ 
    List<Person> list = new List<Person>(); 

    #region Constructors  
    public PersonModel() 
    { 

    }  
    #endregion Constructors) 

    #region Methods  
    public List<Person> GetPersons() 
    { 

     list.Add(new Person("Иванов", "Иван", "Иванович", new DateTime(1980, 5, 10), Gender.Male)); 
     list.Add(new Person("Петр", "Петрович", "Петров", new DateTime(1977, 9, 21), Gender.Male)); 
     list.Add(new Person("Виктория", "Викторовна", "Викторова", new DateTime(1991, 1, 7), Gender.Female)); 

     return list;  
    } 

    public List<Person> SetPersons() 
    { 
     list.Add(new Person("Маташков", "Сергей", "Сергеевич", new DateTime(1980, 5, 10), Gender.Male)); 
     return list;  
    }  
    #endregion Methods  
} 

這是我PersonViewModel PersonsViewModel.cs

public class PersonsViewModel<TViewType> : INotifyPropertyChanged, IViewModel where TViewType : IView, new() 
{ 
    private readonly IView _view; 
    private readonly PersonModel _model; 

    public ObservableCollection<Person> Persons { get; set; } 
    public RelayCommand OkCommand { get; private set; } 

    private string _str; 

    public PersonsViewModel() 
    { 
     this._view = new TViewType(); 
     this._model = new PersonModel(); 
     this.Persons = new ObservableCollection<Person>(this._model.GetPersons()); 
     this.OkCommand = new RelayCommand(o => this.OKRun()); 

     _str = "Кнопка";  

     this._view.SetDataContext(this); 
     this._view.ShowView();    
    } 

    public string Str 
    { 
     get { return _str; } 
     set 
     { 
      if (_str == value) 
       return; 
      _str = value; 
      OnPropertyChanged("Str");  
     } 
    } 

    public ObservableCollection<Person> Observ 
    { 
     get { return Persons; } 
     set 
     { 
      if (Persons == value) 
       return; 
      Persons = value; 
      OnPropertyChanged("Observ");  
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    //[NotifyPropertyChangedInvocator] 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     var handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    }  

    private void OKRun() 
    { 
     Str = "Refresh"; 
     this.Persons = new ObservableCollection<Person>(this._model.SetPersons()); 
    } 
} 

這是我的PersonsView

PersonsView.xaml

<DataGrid ItemsSource="{Binding Observ, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" /> 

我想更新Datadrid?

爲什麼不更新Datagrid和observablecollection?

+1

this.Persons =新的ObservableCollection (this._model.GetPersons()); ?? 這不應該是這個.Observ? – Muds

+0

'{綁定人...'是你需要的。 –

+0

我不能見人集合:P ...可能卜只需要OBSERV重命名爲人員 – Muds

回答

1

在你的構造函數,你應該寫:

this.Observ= new ObservableCollection<Person>(this._model.SetPersons()); 

而不是

this.Persons = new ObservableCollection(this._model.SetPersons());

+0

this.Observ =新的ObservableCollection (this._model.SetPersons ());這不是工作 –

+0

對不起,這是一個很好的決定 –