允许视图在MVP中使用Presenter的Model属性进行数据绑定 [英] Allowing View To Use Presenter's Model Property For Databinding in MVP

查看:187
本文介绍了允许视图在MVP中使用Presenter的Model属性进行数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Winform模型视图演示器(MVP)应用程序,该应用程序具有在实例化时通过接口传入的Presenter对象.演示者(ProductPresenter)具有模型(ProductModel)属性.我允许视图(ProductView)访问ProductPresenter的ProductModel属性来设置数据绑定.在加载事件期间,ProductView下方使用其ProductPresenter实例访问其ProductModel的"City"属性来设置数据绑定到ProductView上名为textBoxCity的文本框控件:

I have a Winform Model View Presenter (MVP) application which has a Presenter object passed in at instantiation time via an interface. The Presenter (ProductPresenter) has a model (ProductModel) property. I am allowing the View (ProductView) to access the ProductPresenter's ProductModel property to setup databinding. 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:

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

所有带有getter的设置和带有InotifyPropertyChanged的setter的设置都是在ProductPresenter中使用其ProductModel属性定义的.

All the setup with getters and setters with INotifyPropertyChanged is defined in the ProductPresenter using its ProductModel property.

我对此唯一担心的是,我必须使View知道演示者.在此之前,我的View不需要知道Presenter,因为它正在引发Presenter订阅的事件,以使Presenter知道正在发生的事情,并有机会根据View的观点来告诉View.关于收到的事件通知.我找不到一种简单的方法来允许ProductPresenter使用事件通知将其ProductModel属性数据绑定到ProductView文本框,而视图没有引用ProductPresenter.

My only concern in this is that I had to make the View aware of the presenter. 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. 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.

除了上面为WinForms MVP所描述的维护和支持实用方法以外,还有另一种方法可以实现对View和关联模型的数据绑定.如果有人知道任何问题,上述方法将引起注意.提前致谢.

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. 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));

这篇关于允许视图在MVP中使用Presenter的Model属性进行数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆