首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在WPF MVVM中使用ICollectionView更新ICollectionView

在WPF MVVM中使用ICollectionView更新ICollectionView
EN

Stack Overflow用户
提问于 2017-01-13 11:46:28
回答 1查看 771关注 0票数 1

我有个相当有趣的问题。我在wpf中有一个DataGrid,如下所示:

代码语言:javascript
复制
<DataGrid ItemsSource="{Binding View, IsAsync=True, Mode = TwoWay}"
          AutoGenerateColumns="False" 
          EnableColumnVirtualization="True" 
          EnableRowVirtualization="True"
          VirtualizingStackPanel.VirtualizationMode="Standard"
          VirtualizingStackPanel.IsVirtualizing="True">
     COLUMNS
</DataGrid>

在该网格中的数据上,我正在执行crud操作,但是在添加或删除操作之后,我似乎无法刷新视图,当我更新记录或过滤它时,它会完美地工作。

我在视图模型中尝试过简单的C#操作。

阅读:

代码语言:javascript
复制
    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);
    }

并增加:

代码语言:javascript
复制
    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命令中刷新视图,但是它没有工作。有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-13 11:53:40

绑定到CommendationView:

代码语言:javascript
复制
<DataGrid ItemsSource="{Binding CommendationView}" ...

...and确保此属性的设置程序引发PropertyChanged事件:

代码语言:javascript
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41633954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档