简体   繁体   English

在 WPF MVVM 中使用 ICollectionView 更新 DataGrid

[英]Updating DataGrid using ICollectionView in WPF MVVM

I have rather interesting problem.我有一个相当有趣的问题。 I have a DataGrid in wpf that looks like:我在 wpf 中有一个 DataGrid,它看起来像:

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

On data in that grid I am performing crud operations, but it seems I cannot refresh view after Add or Delete operation, it works perfectly when I Update record or filter it.在该网格中的数据上,我正在执行 crud 操作,但在添加或删除操作后似乎无法刷新视图,当我更新记录或过滤它时,它工作得很好。

Simple C# ops I tried in view model.我在视图模型中尝试过的简单 C# 操作。

Read:读:

    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);
    }

And add:并添加:

    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");
    }

As you see I tried to Refresh view in add command but it did not work.如您所见,我尝试在添加命令中刷新视图,但没有奏效。 Any suggestions?有什么建议?

Bind to CommendationView:绑定到 CommendationView:

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

...and make sure that the setter of this property raises the PropertyChanged event: ...并确保此属性的设置器引发 PropertyChanged 事件:

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

The view model class must implement the INotifyPropertyChanged interface for this to work: https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx视图模型类必须实现 INotifyPropertyChanged 接口才能工作: https : //msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v= vs.110).aspx

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

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