Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ObservableCollection未更新_C#_Wpf_Mvvm - Fatal编程技术网

C# ObservableCollection未更新

C# ObservableCollection未更新,c#,wpf,mvvm,C#,Wpf,Mvvm,我有这个密码 这是我的个人模型 PersonModel.cs public class PersonModel { List<Person> list = new List<Person>(); #region Constructors public PersonModel() { } #endregion Constructors) #region Methods publi

我有这个密码

这是我的个人模型 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       
}
公共类PersonModel
{
列表=新列表();
#区域构造函数
公众人物模型()
{
}       
#endregion构造函数)
#区域方法
公众人士名单
{
列表。添加(新人物(“ааааа”、“аааа”、“ааааач”,新日期时间(1980年5月10日),性别。男性);
列表。添加(新人物(“Пчч”、“Пчч”、“Пччч”),新日期时间(1977年9月21日),性别。男性);
列表。添加(新的人(“аааа”、“аааааа”、“ааааааааа107;
退货清单;
}
公众人士名单(
{
列表。添加(新人物(“Мааааааа”、“Сааа”、“Саааааааа;
退货清单;
}      
#端域法
}
这是我的PersonViewModel PersonViewModel.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());
   }
}
公共类PersonViewModel:INotifyPropertyChanged,IViewModel其中TViewType:IView,new() { 私有只读IView\u视图; 私有只读个人模型; 公共可观察集合人员{get;set;} public RelayCommand OkCommand{get;private set;} 私有字符串_str; 公共人员视图模型() { 这是。_view=new TViewType(); 这个._model=newpersonmodel();
this.Persons=新的ObservableCollection(this.\u model.GetPersons()); this.OkCommand=newrelaycommand(o=>this.OKRun()); _str=“КаПа”; this.\u view.SetDataContext(this); 这是。_view.ShowView(); } 公共字符串Str { 获取{return\u str;} 设置 { 如果(_str==值) 返回; _str=值; 不动产变更(“Str”); } } 公共观测收集观测 { 获取{返回人员;} 设置 { 如果(人员==值) 返回; 人=价值; 不动产变更(“观察者”); } } 公共事件属性更改事件处理程序属性更改; //[NotifyPropertyChangedInvocator] 受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null) { var handler=PropertyChanged; if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName)); } 私有void-OKRun() { Str=“刷新”;
this.Persons=新的ObservableCollection(this.model.SetPersons()); } } 这是我个人的看法

PersonView.xaml

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

我想要更新的数据驱动


为什么不更新Datagrid和observablecollection?

在构造函数中,您应该写:

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

this.Observ=新的observeCollection(this.\u model.SetPersons());
而不是


this.Persons=新的ObservableCollection(this.\u model.SetPersons())

在构造函数中,您应该编写:

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

this.Observ=新的observeCollection(this.\u model.SetPersons());
而不是


this.Persons=新的ObservableCollection(this.\u model.SetPersons())

this.Persons=新的ObservableCollection(this._model.GetPersons())??如果不是这样的话。Observ?
{Binding Persons…
就是你需要的。我看不到Persons集合:P…你可以把Observ重命名为Persons吗{Binding Persons}不工作Persons is observecollection this.Persons=新的observecollection(this.\u model.GetPersons())?这不是这个吗?Observ?
{Binding Persons…
是您需要的。我看不到Persons集合:P…您只需要将observ重命名为Persons{Binding Persons}不工作Persons是observecollectionthis.observ=new observecollection(this.\u model.SetPersons());它不工作this.observ=new observecollection(this.\u model.SetPersons());它不工作