1

I am using MVP in my project, but I am new in MVP. I have two comboboxes. When I select an option in a combobox, the other combobox should be filled with new data.

This action will be done in the Presenter. I got my view 'view1' in Presenter, and introduced Combobox1 and Combobox2 as properties in 'view1', because I needed 'DataSource', 'DisplayMember', 'ValueMember' and 'Refresh()' in the method below.

But, when using a pattern, it is enough to send a property like this:

public string Combobox2
{
    get { return comboBox1.SelectedValue.ToSstring(); }
}

into the Presenter, not the whole Combobox. How can I solve this problem?

public void OnSelectedIndexChangedCombobox1()
{
    if (view1.Combobox1.SelectedIndex == -1)
    {
        return;
    }

    DataTable dt = Tools.GetDataTable("A Path");

    var query =
        (from o in dt.AsEnumerable()
         where o.Field<string>("afield") == 
             farmerView.Combobox1.SelectedValue.ToString()
         orderby o.Field<string>("anotherfield")
         select new KeyValuePair<string, string>(o.Field<string>("field1"), 
             o.Field<string>("field2"))).ToList();

    farmerView.Combobox2SelectedIndexChanged -= OnSelectedIndexChangedCombobox2;

    farmerView.Combobox2.DataSource = new BindingSource(query, null);
    farmerView.Combobox2.DisplayMember = "Value";  
    farmerView.Combobox2.ValueMember = "Key";   
    farmerView.Combobox2.Refresh();
    farmerView.Combobox2SelectedIndexChanged += 
       OnSelectedIndexChangedCombobox2;

    farmerView.Combobox2.SelectedIndex = -1;
}

Update:

I used this method:

public void OnSelectionChangeCommittedCombobox1()
{
    if (View1.Combobox1SelectedIndex == -1)
    {
        return;
    }

    DataTable dt = Tools.GetDataTable("A Path");

    var query =
        (from o in dt.AsEnumerable()
         where o.Field<string>("afield") == 
             farmerView.Combobox1.SelectedValue.ToString()
         orderby o.Field<string>("anotherfield")
         select new KeyValuePair<string, string>(o.Field<string>("field1"), 
             o.Field<string>("field2"))).ToList();

    View1.DataSource = new BindingSource(query, null);
    View1.DisplayMember = "Value";  
    View1.ValueMember = "Key";   
    View1.Refresh();

    View1.Combobox2SelectedIndex = -1;
}

Now I do not have direct access to the comboboxes.

1

What you call Presenter is actually the View. The separate View class you have is unnecessary.

The View takes the data it has available and binds it to its UI elements.

What a Presenter would do, if you had one, is to present the data in such a way that it can easily be bound by the View.

In MVP the View knows the Presenter and the Presenter knows the Model, but the Model doesn't know the Presenter and the Presenter doesn't know the View.

This is the Presenter:

    DataTable dt = Tools.GetDataTable("A Path");

    this.AProperty =
        (from o in dt.AsEnumerable()
         where o.Field<string>("afield") == 
             farmerView.Combobox1.SelectedValue.ToString()
         orderby o.Field<string>("anotherfield")
         select new KeyValuePair<string, string>(o.Field<string>("field1"), 
             o.Field<string>("field2"))).ToList();

This is the View:

    this.DataSource = new BindingSource(presenter.AProperty, null);
    this.DisplayMember = "Value";  
    this.ValueMember = "Key";   
    this.Refresh();

    this.Combobox2SelectedIndex = -1;
0

Your presenter is doing too much. Your presenter should never have any references to view information (in this case the Combo Box control).

The view is responsible for getting information from the user and then taking the model formatting it for the user based on the view.

The presenter takes information from the view and does something with it. In this case get some data. That data should be returned to the view so it can present it on how the view feels is the right way.

The model is the information that is being passed back and forth.

What you need to do is pretend you are not just using this from a Windows application but you also have to use it form a console as well. Since a console will have no reference to a combo-box then you cannot have it in your presenter.

You should read this article on architecture. Notice that the UI is on the outer ring. You need to think of the UI as interchangeable. If you cannot switch your UI from a console, WPF, WinForms, Web Service, Web Site then your presenter is designed incorrectly. This is where unit testing will come into play. If you unit test your presenter then you will be forced to implement it in a way that makes it interchangeable.

  • Thank you for very good information. I am writing another method. I will share it. – Mohsen Apr 9 '17 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.