【问题标题】:How to update gridview datasource binding at runtime in mvvm framework?如何在 mvvm 框架中在运行时更新 gridview 数据源绑定?
【发布时间】:2018-06-27 01:53:15
【问题描述】:

我有 MainModelList 对象与 gridview 数据源的绑定列表,当我单击按钮 barButtonItemValidate 时,我已经更新了 MainModel 列表中的对象,但 gridview 数据源没有更新或刷新。 那么如何在运行时更新gridview数据源呢?

    //View
    _fluent = mvvmContext.OfType<MainViewModel>();
    _fluent.SetBinding(gridControlView, g => g.DataSource, x => x.MainObjectList);
    _fluent.BindCommand(barButtonItemValidate, x => x.Update());
    //ViewModel
    public MainModelList MainObjectList { get; set; }
    public MainViewModel()
    {
        MainObjectList = new MainModelList();
    }

【问题讨论】:

    标签: c# winforms mvvm devexpress


    【解决方案1】:

    如果您要替换整个列表。将 MainObjectList 属性设为虚拟,使其成为 INotifyPropertyChanged aware

    // ViewModel
    public virtual MainModelList MainObjectList { 
        get; 
        protected set; // changing allowed from the ViewModel's side only
    }
    

    此更改允许通过绑定执行网格控件的数据源更新。

    如果您要更改 MainObjectList 集合中的单个对象,请确保这些更改在整个集合和/或特定集合的项目级别提供通知
    完成此任务的最简单方法是从 T 实现 INotifyPropertyChanged 接口的 BindingList 派生您的集合:

    public class MainObjectList : BindingList<MainObject> {
        // ....
    }
    public class MainObject : INotifyPropertyChanged {
        // ...
    }
    

    BindingList 将跟踪包含的对象并引发ListChanged 通知。这些通知将由 gridView 处理,因此,gridView 的显示值将被更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 2015-04-29
      • 1970-01-01
      • 2013-07-08
      • 2012-08-13
      • 1970-01-01
      相关资源
      最近更新 更多