如何以MVC方式挂接Data Binding、Linq、DataGridView?

发布于 2024-12-06 16:47:08 字数 1504 浏览 0 评论 0原文

我想以 MVC 方式尽可能地将表示逻辑和控件分开:

class MyModel : INotifyPropertyChanged { 

    private IEnumerable<Domain> _domains;
    public IEnumerable<Domain> Domains { 
        get { return _domains; } 
        set { _domains = value; SendPropertyChanged("Domains");
    }
}

class MyControl 
{ 
    // m_Grid's hooked up to m_BindingSource 
    private DataGridView m_Grid;
    private BindingSource m_BindingSouce;
    public void SetModel( MyModel model )
    {
        m_BindingSource.DataSource = model.Domains;
    }
}

class Controller
{
    private MyModel _model;
    private void UpdateDomains()
    {
        // predicate is built on user inputs
        _model.Domains = db.GetDomains( predicate );               
    }
}

// extra code to create Controller, MyModel, and MyControl.  

当 MyModel.Domains 更改时,会触发一个事件来通知 MyControl 它的 m_BindingSource.DataSource 已更改。但是,网格不会使用新值进行更新。 为了解决这个问题,MyControl 被更改为:

class MyControl 
{        
    public void SetModel( MyModel model )
    {
        m_BindingSource.DataSource = model.Domains;
        model.Domains.PropertyChanged += OnDataChanged;          
    }
    private void OnDataChanged(object sender, PropertyChangedEventArgs e)
    {
        BindingSource.DataSource = _model.Data.Users;
    }
}

任何人都可以解释为什么需要这个吗? DataBinding 的主要好处之一是减轻程序员管理 PropertyChanged 事件的负担。具有讽刺意味的是,BindingSouce 能够检测 MyModel.Domains 上的更改。我尝试更新 m_BindingSource.DataSourceChanged 处理程序内的数据源。更改后,网格停止更新。

I'd like to separate the presentation logic and the controls as much as possible in a MVC fashion:

class MyModel : INotifyPropertyChanged { 

    private IEnumerable<Domain> _domains;
    public IEnumerable<Domain> Domains { 
        get { return _domains; } 
        set { _domains = value; SendPropertyChanged("Domains");
    }
}

class MyControl 
{ 
    // m_Grid's hooked up to m_BindingSource 
    private DataGridView m_Grid;
    private BindingSource m_BindingSouce;
    public void SetModel( MyModel model )
    {
        m_BindingSource.DataSource = model.Domains;
    }
}

class Controller
{
    private MyModel _model;
    private void UpdateDomains()
    {
        // predicate is built on user inputs
        _model.Domains = db.GetDomains( predicate );               
    }
}

// extra code to create Controller, MyModel, and MyControl.  

When MyModel.Domains is changed, an event is fired to inform MyControl that its m_BindingSource.DataSource is changed. However, the grid isn't updated with the new values.
To fix this problem, MyControl is changed to:

class MyControl 
{        
    public void SetModel( MyModel model )
    {
        m_BindingSource.DataSource = model.Domains;
        model.Domains.PropertyChanged += OnDataChanged;          
    }
    private void OnDataChanged(object sender, PropertyChangedEventArgs e)
    {
        BindingSource.DataSource = _model.Data.Users;
    }
}

Can anyone explain why is this needed? One of the main benefits of DataBinding is to alleviate programmers from managing PropertyChanged events. Ironically, BindingSouce is able to detect changes on MyModel.Domains. I experimented updating the DataSource inside a handler of m_BindingSource.DataSourceChanged. After the change, the grid stops updating.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落星ぅ辰 2024-12-13 16:47:08

查看 ObservableCollection 是否 http://msdn.microsoft.com/en-us/library /ms668604.aspx
或 BindingList http://msdn.microsoft.com/en-us/library/ms132679 .aspx 有任何区别。

顺便说一句,你打错了(忘记了“r”):

private BindingSource m_BindingSouce; 

See if an ObservableCollection http://msdn.microsoft.com/en-us/library/ms668604.aspx
or BindingList http://msdn.microsoft.com/en-us/library/ms132679.aspx makes any difference.

btw you made a typo (forgot the 'r'):

private BindingSource m_BindingSouce; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文