2017-01-13 30 views
1

私はむしろ面白い問題があります。私はのように見えることWPFのデータグリッドを持っている:私はCRUD操作を行っており、そのグリッド内のデータにWPFでICollectionViewを使ってDataGridを更新するMVVM

<DataGrid ItemsSource="{Binding View, IsAsync=True, Mode = TwoWay}" 
         AutoGenerateColumns="False" 
         EnableColumnVirtualization="True" 
         EnableRowVirtualization="True" 
         VirtualizingStackPanel.VirtualizationMode="Standard" 
         VirtualizingStackPanel.IsVirtualizing="True"> 
    COLLUMNS 
</DataGrid> 

、私が操作を追加または削除した後に表示を更新することができないようで、私はレコードやフィルタを更新したときにそれが完璧に動作しますそれ。私は、ビューモデルにしようとした

単純なC#のOPS: 読む:

public CommendationViewModel() 
    { 
     this._catalog = new CatalogContexct(); 
     this._commendations = this._catalog.Commendations.ToList(); 

     var commendation = new ListCollectionView(this._commendations); 
     this.CommendationView = CollectionViewSource.GetDefaultView(commendation); 

     this.AddCommand = new RelyCommand(AddEntity, param => this._canExecute); 
     this.EditCommand = new RelyCommand(EditEntity, param => this._canExecute); 
     this.UpdateCommand = new RelyCommand(UpdateEntity, param => this._canExecute); 
     this.RemoveCommand = new RelyCommand(RemoveEntity, param => this._canExecute); 

     this.NameCommand = new RelyCommand(Filter, param => this._canExecute); 
     this.CancelCommand = new RelyCommand(Cancel, param => this._canExecute); 
    } 

そして追加:

public void AddEntity(object obj) 
    { 
     if(string.IsNullOrEmpty(this.Name)) 
     { 
      MessageBox.Show("Brak nazwy do dodania"); 
      return; 
     } 
     var commendation = new Commendation() { Name = this.Name }; 
     this._catalog.Commendations.Add(commendation); 
     this._catalog.SaveChanges(); 

     var commendationRefresh = new ListCollectionView(this._catalog.Commendations.ToList()); 
     this.CommendationView = CollectionViewSource.GetDefaultView(commendationRefresh); 
     this.CommendationView.Refresh();    

     MessageBox.Show("Nowe źródło polecenia zostało dodane"); 
    } 

あなたは私がaddコマンドでビューを更新しようとしたが、それはうまくいきませんでした見たよう

。助言がありますか?

+0

DataGridのItemsSourceプロパティを "View"というプロパティにバインドしましたが、 "Co"というプロパティをリフレッシュしていますmmendationView "と表示されます。 DataGridがバインドされているのと同じビューを更新する必要があります。 – mm8

答えて

1

CommendationViewにバインド:

<DataGrid ItemsSource="{Binding CommendationView}" ... 

...と、このプロパティのセッターがPropertyChangedイベントを発生させていることを確認してください:

private ICollectionView _commendationView; 
public ICollectionView CommendationView 
{ 
    get { return _commendationView; } 
    set { _commendationView = value; NotifyPropertyChanged(); } 
} 

ビューモデルクラスは、このためのINotifyPropertyChangedのインターフェイスを実装する必要がありますhttps://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

関連する問題