本文介绍了MVP winform中的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WinForms应用程序在MVP中实现。我的表单有一个 TextBox ,我想将其$ code> Text 属性数据绑定到模型中的属性。我不想参考视图中的模型。



在Google中搜索后,我发现通过耦合模型和视图进行数据绑定是一个坏主意。我的样本初始化模型查看 Presenter

  class View:Form,IView 
{
public View()
{
InitializeComponent();
new Presenter(this);
}
}

class Presenter
{
public Presenter(IView):this.Presenter(this,new Model())
{
}

public Presenter(IView view)
{
}
}

类型号:IModel
{
public Model()
{
}

}

目前我有3个项目,分别为模型查看演示者。视图引用 Presenter Presenter 引用模型 。任何人都可以指导我如何在中查看中的控件的数据绑定到 Model

中的属性

编辑



我知道在Grid中做的事情。我们可以将数据源属性赋值给演示者中的列表(或类似的东西),如:

  _view.DataSource = _model.ListOfEmployees; 

ListOfEmployees 模型中的变化。但是,一个 TextBox 会暴露一个 Text 属性呢?我如何在MVP架构中绑定?

解决方案

我的建议是将View和Model封装在Presenter中。这意味着给定View的专门的Presenter(在大多数情况下)。在我看来,这是很好的,因为大多数模型将会有所不同。

  class Presenter {
readonly IView view ;
只读IModel模型;

public Presenter(){
//如果视图需要参考。演示者,传入视图ctor
view = new View(this);
model = new Model();
}

//或者 - 模型注入 - 我的偏好
public Presenter(IModel Model){
// if view needs ref。演示者,传入视图ctor
view = new View(this);
model = Model;
}
}

在您的IView中,公开控件或控件的数据源属性:

 接口IView {
对象GridDataSource {get;组;
}

添加到您的Presenter一些方法:

  void SetGridDatasource(){
view.GridDatasource = model.SomeBindableData;
}

查看实现:

  public object GridDatasource {
get {return myGridView.DataSource; }
set {myGridView.DataSource = value; }
}

注意:

代码片段未经测试,并推荐作为起点。



更新到评论

INotifyPropertyChanged 是一个非常 IView IModel 之间的属性更新的有价值的机制。



DataBinding方法尽可能的。简单地通过IView公开这些属性,并让Presenter将这些绑定设置为IModel属性。


I have a WinForms application implemented in MVP. My form has a TextBox and I want to databind its Text property to a property in the Model. I don't want to refer to the Model in the View.

After searching in Google, I found that databinding by coupling Model and View is a bad idea. My sample initialization of Model, View and Presenter is as follows.

class View : Form, IView
{
    public View()
    {
        InitializeComponent();
        new Presenter(this);
    }
}

class Presenter
{
    public Presenter(IView) : this.Presenter(this, new Model())
    {
    }

    public Presenter(IView view)
    {
    }
}

class Model : IModel
{
    public Model()
    {
    }

}

At present I have 3 projects each for Model, View and Presenter. View has reference to Presenter and Presenter has reference to Model. Can anyone guide me how to form a databinding to a control in View to a property in Model?

EDIT

I know to do the things in Grid. We can assign Datasource property of grid to a List (or something similar) in presenter like:

_view.DataSource = _model.ListOfEmployees;

This will reflect the value in UI when ListOfEmployees changes in the Model. But what about a TextBox which exposes a Text property? How can I bind that in MVP architecture?

解决方案

My recommendation is to encapsulate the View and Model in the Presenter. This means a specialized Presenter (in most cases) for a given View. In my opinion, this works out well since most Models will be different anyway.

class Presenter {
    readonly IView view;
    readonly IModel model;

    public Presenter() {
        // if view needs ref. to presenter, pass into view ctor
        view = new View(this);
        model = new Model();
    }

    // alternatively - with the model injected - my preference
    public Presenter(IModel Model) {
        // if view needs ref. to presenter, pass into view ctor
        view = new View(this);
        model = Model;
    }
}

In your IView, expose a control or control's data source property:

interface IView {
    object GridDataSource { get; set; }
}

Add to your Presenter some method:

void SetGridDatasource() {
    view.GridDatasource = model.SomeBindableData;
}

View implementation:

public object GridDatasource {
    get { return myGridView.DataSource; }
    set { myGridView.DataSource = value; }
}

Note:
Code snippets are untested and recommended as a starting point.

Update to comments:
INotifyPropertyChanged is a very valuable mechanism for updating properties between IView and IModel.

Most controls do have some sort of binding capability. I would recommend using those DataBinding methods whenever possible. Simply expose those properties through IView and let the Presenter set those bindings to the IModel properties.

这篇关于MVP winform中的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 05:27