2013-03-20 143 views
2

我仍然在WPF,EF和MVVM的学习阶段,现在我得到了以下Probplem。我可以在我的DataGridView中删除和插入新项目,但我不知道如何更新我的项目。我所做的只是选择一个已经获得主键的空行,然后将数据放入其中。它正在工作(更新数据库),但GridView不刷新。我需要先重新启动程序才能看到我的更新数据。EF更新不更新GridView

我的执行命令来更新我的数据库。我在ViewModel类

 public void ExecuteUpdate(object obj) 
     { 
      try 
      { 

       SelectedIndex.Child_Update(new Farbe { FarbauswahlNr = SelectedIndex.FarbauswahlNr, Kurztext = SelectedIndex.Kurztext, Ressource = SelectedIndex.Ressource, Vari1 = SelectedIndex.Vari1, Vari2 = SelectedIndex.Vari2 }); 
       //ListeAktualisieren --> Refreshing the List 
       ListeAktualisieren();      
      } 
      catch (Exception e) 
      { 
       MessageBox.Show(e.ToString()); 
      } 
     } 

这是我的Regresh方法至少应该刷新GridView。林在ViewModel类

 public void ListeAktualisieren() 
     { 


      farbliste.ListeAktualisieren(db); 
      farbliste.Model = farbliste.Model.Concat(farbliste.Addlist).ToList(); 
      Model = farbliste.Model; 
      farbliste.Addlist.Clear(); 
     } 

该方法被调用我的企业名单至极也得到了Refreh方法。在这里从我的数据库读取。 IM在商户列表类

public void ListeAktualisieren(TestDBEntities db) 
    { 
     Model.Clear(); 
     foreach (var item in db.Farben) 
     { 
      //Insert and delete working 
      add = new Farbe { FarbauswahlNr = item.FarbauswahlNr, Kurztext = item.Kurztext, Ressource = item.Ressource, Vari1 = Convert.ToBoolean(item.Var1), Vari2 = item.Vari2 }; 
      Addlist.Add(add);    
     }   

    } 

型号是我的GridView至极的来源是不是清爽更新,但插入或删除时,显示新的数据行时更改的数据。

回答

2

您需要具有实现INotifyPropertyChanged的Observablecollections和Classes。通过插入并通过更改引发event属性更改,将新元素添加到Observablecollection中。 其余的应该由WPF完成。

编辑:DataGrid的Sourcecollection需要是Observablecollection。


EDIT2:是好的,我把意见在这里;-) DataGrid的每一行是集合的元素的结果。一行的每个单元格侦听其元素的PropertyChangedEvent(该字符串是区分大小写的,因此请小心)。如果属性的getter在propertychangedevent之后未被调用,则绑定没有收到事件。 这片代码可以帮助可以确定地说你不不存在的字符串拨打:

private void VerifyPropertyName(string PropertyName) 
{ 
    if (string.IsNullOrEmpty(PropertyName)) 
     return; 
    if (TypeDescriptor.GetProperties(this)(PropertyName) == null) { 
     string msg = "Ungültiger PropertyName: " + PropertyName; 
     if (this.ThrowOnInvalidPropertyName) { 
      throw new isgException(msg); 
     } else { 
      Debug.Fail(msg); 
     } 
    } 
} 
+0

我的物业有一个OnPropertyChange。 – 2013-03-20 13:45:00

+0

PropertyChangedEvent必须到达WPF层你的连接从ViewModel到WPF如何? – Patrick 2013-03-20 13:54:01

+0

Farbliste.cs和Farbe.cs是CSLA classen所以我不认为有一个Observablecollection需要? – 2013-03-20 14:09:00

0

尝试添加以下内容到绑定部分 的ItemsSource =“{绑定路径=型号,UpdateSourceTrigger =的PropertyChanged”}

+0

已经做到了。忘了编辑它 – 2013-03-21 08:16:49