【问题标题】:Devexpress winforms bind GridControl Selected row to a property in ViewModelDevexpress winforms 将 GridControl Selected 行绑定到 ViewModel 中的属性
【发布时间】:2014-03-23 15:26:02
【问题描述】:

我正在使用 devexpress GridControl 开发一个 winforms 应用程序,并且我有一个带有两个端口的 ViewModel

public class MyViewModel
{
    public List<Cusomer> Customers{get;set;}
    public Customer SelectedCustomer {get;set;}

}

如何将 GridControl 的 SelectedRow 绑定到我的 ViewModel SelectedCustomer porperty?

谢谢

【问题讨论】:

    标签: c# winforms binding devexpress gridcontrol


    【解决方案1】:

    不确定是否有直接绑定属性的方法。您可以改为捕获更改选择时在BindingSource 中触发的事件。

    1) 为CurrentChanged 事件添加事件处理程序:

    public class MyViewModel
    {
        public List<Customer> Customers { get; set; }
        public Customer SelectedCustomer { get; set; }
    
        public void BindingSourceCurrentChanged(object sender, EventArgs e)
        {
            var bindingSource = sender as BindingSource;
            if (bindingSource == null) return;
    
            SelectedCustomer = bindingSource.Current as Customer;
        }
    }
    

    2) 将 GridControl、BindingSource 和 ViewModel 链接在一起:

    BindingSource bindingSource = new BindingSource { DataSource = myViewModel.Customers };
    bindingSource.CurrentChanged += myViewModel.BindingSourceCurrentChanged;
    gridControl1.DataSource = bindingSource;
    gridView1.PopulateColumns();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多