lundi 26 octobre 2015

WinForms - grouping controls in collection - MVP

BACKGROUND

I am trying to write simple WinForms application, using MVP structure in my project. Now I am trying to bind lists to group of ComboBoxes. My form looks like this:

enter image description here

It implements interface IMaterialDatabasePropertiesView:

public interface IMaterialDatabasePropertiesView
{
    MaterialDatabasePropertiesPresenter Presenter { set; }

    List<string> ElementsList1st { get; set; }
    int SelectedElement1st { get; set; }

    List<string> ElementsList2nd { get; set; }
    int SelectedElement2nd { get; set; }
    .
    .
    //etc... until 14th elementh is reached
}

Implementation of that intefrace is very simple:

public partial class MaterialDatabasePropertiesForm : Form, IMaterialDatabasePropertiesView
{
    public MaterialDatabasePropertiesPresenter Presenter { private get; set; }

    public List<string> ElementsList1st
    {
        get { return this.comboBox1st.DataSource as List<string>;  }
        set { this.comboBox1st.DataSource = value;  }
    }
    public int SelectedElement1st
    {
        get { return this.comboBox1st.SelectedIndex; }
        set { this.comboBox1st.SelectedIndex = value; }
    }

    public List<string> ElementsList2nd
    {
        get { return this.comboBox2nd.DataSource as List<string>;  }
        set { this.comboBox2nd.DataSource = value; }
    }
    public int SelectedElement2nd
    {
        get { return this.comboBox2nd.SelectedIndex; }
        set { this.comboBox2nd.SelectedIndex = value; }
    }
    .
    .
    //etc... until 14th elementh is reached
}

What I want to do is binding to each comboBox list of that Enum:

public class MaterialDatabasePropertiesPresenter
{
        private readonly IMaterialDatabasePropertiesView _view;
        private List<FormComboBox> ComboBoxes;

        public enum OfElement
        {
            C, Si, Mn, P, S,
            N, Cr, Mo, Nb, Ni,
            Ti, Al, V, Cu
        }

private List<OfElement> listOfElements = Enum.GetValues(typeof(OfElement))
                                 .Cast<OfElement>()
                                 .ToList();
}


PROBLEM DESCRIPTION

What I need to do is bind that list to each comboBox under certain cimcurstances:

1) If no comboBox is filled you can choose from all enums.

2) If some comboBox is set to some specific enum, another no should not let me to choose the same enum (simply it shouldn' t even appear on list of avalible options in comboBox

Now I am trying to all of these controls in one list, or any other kind of Collection, so it would make my task easier. I created this class:

private class FormComboBox
    {
        public FormComboBox(List<string> avalibleElements, int currentIndex)
        {
            AvalibleElements = avalibleElements;
            CurrentIndex = currentIndex;
        }

        public List<string> AvalibleElements;
        public int CurrentIndex;
    }

And put List<FormComboBox> ComboBoxes; as property in my Presenter class. I Initialize it like that:

private void InitializeComboBoxesProperty()
        {
            ComboBoxes = new List<FormComboBox>()
            {
                new FormComboBox(_view.ElementsList1st, _view.SelectedElement1st),
                new FormComboBox(_view.ElementsList2nd, _view.SelectedElement2nd),
                .
                .
                //etc. till 14th comboBox
            };
        }

Of course when I change some element of ComboBox list it only changes that element, and has no influence on my _view.ElementList1st or any other.


Conclusion

To sum up - as you can see I am a bit lost, so I hope you can help me find some solution. To stay clear - I am looking for some kind of design pattern, or something similar, that would let me to group my controls in some collection. Cheers!

Aucun commentaire:

Enregistrer un commentaire