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

≯℡__Kan透↙ 提交于 2019-12-11 17:00:33

问题


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

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

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.

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.


回答1:


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


来源:https://stackoverflow.com/questions/49618011/allowing-view-to-use-presenters-model-property-for-databinding-in-mvp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!