如何以MVC方式挂接Data Binding、Linq、DataGridView?
我想以 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 ObservableCollection 是否 http://msdn.microsoft.com/en-us/library /ms668604.aspx
或 BindingList http://msdn.microsoft.com/en-us/library/ms132679 .aspx 有任何区别。
顺便说一句,你打错了(忘记了“r”):
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'):