简体   繁体   English

允许视图在MVP中使用Presenter的Model属性进行数据绑定

[英]Allowing View To Use Presenter's Model Property For Databinding in MVP

I have a Winform Model View Presenter (MVP) application which has a Presenter object passed in at instantiation time via an interface. 我有一个Winform模型视图演示器(MVP)应用程序,该应用程序具有在实例化时通过接口传入的Presenter对象。 The Presenter (ProductPresenter) has a model (ProductModel) property. 演示者(ProductPresenter)具有模型(ProductModel)属性。 I am allowing the View (ProductView) to access the ProductPresenter's ProductModel property to setup databinding. 我允许视图(ProductView)访问ProductPresenter的ProductModel属性来设置数据绑定。 Below the ProductView is using its instance of ProductPresenter to access its ProductModel "City" property to setup a databind to a textbox control called textBoxCity on the ProductView during the load event: 在ProductView之下,正在使用其ProductPresenter实例访问其ProductModel的“ City”属性,以在加载事件期间在ProductView上将数据绑定设置到名为textBoxCity的文本框控件:

        textBoxCity.DataBindings.Add(
    new Binding("Text", this.ProductPresenter.ProductModel, 
"City", false, DataSourceUpdateMode.OnPropertyChanged));

All the setup with getters and setters with INotifyPropertyChanged is defined in the ProductPresenter using its ProductModel property. 使用getter和使用InotifyPropertyChanged的setter进行的所有设置都在ProductPresenter中使用其ProductModel属性进行定义。

My only concern in this is that I had to make the View aware of the presenter. 我对此唯一担心的是,我必须使View知道演示者。 Prior to this, my View did not need to be aware of the Presenter because it was raising events which the Presenter subscribed to in order to allow the Presenter to know what was going on and giving it a chance to tell the View what to do based on received event notifications. 在此之前,我的View不需要知道Presenter,因为它正在引发Presenter订阅的事件,以使Presenter知道正在发生的事情,并有机会告诉View要做的事情。关于收到的事件通知。 I was not able to find an easy way to allow the ProductPresenter to have its ProductModel properties databind to the ProductView textBoxes using event notifications with the View having no reference to the ProductPresenter. 我找不到一种简单的方法来允许ProductPresenter使用事件通知将其ProductModel属性数据绑定到ProductView文本框,而视图没有引用ProductPresenter。

Is there another way to achieve databinding to a View and associated model other than what is described above for WinForms MVP that is practical to maintain and support. 除了以上针对WinForms MVP所描述的,易于维护和支持的方法以外,还有另一种方法可以实现对View和关联模型的数据绑定。 If anyone is aware of any problems the above approach will create please comment. 如果有人意识到上述问题,请使用上面的方法发表评论。 Thanks in advance. 提前致谢。

View should not know about model or presenter. 视图不应了解模型或演示者。 Handle the data binding in the presenter: 处理演示者中的数据绑定:

    View.textBoxCity.DataBindings.Add(
            new Binding("Text",ProductModel, 
            "City", false, DataSourceUpdateMode.OnPropertyChanged));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM