Sverre

Sverre

  • NA
  • 53
  • 0

Databinding interface property - Expert challange!

Aug 20 2008 5:00 AM
Hi!

I was recommended by a friend to try this site, since there were a lot of expert programmers here, so I am crossing my fingers that some of you have a clue of what I am doing wrong here.

I have a solution with 3 classes; Form1 (regular Form), IPerson (interface) and Person (class implementing IPerson). The form has one control, a ComboBox, which should be databound to a property in the form (CurrentPerson). The SelectedIndexChanged event in the ComboBox makes sure the databinding writes its value every time the selected item is changed. The code is like this:

IPerson:
    public interface IPerson
    {
        string Name { get; set; }
    }

Person:
    public class Person : IPerson
    {
        public Person(string name)
        {
            Name = name;
        }

        public string Name { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }

Form1:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            IPerson p1 = new Person("Person 1");
            IPerson p2 = new Person("Person 2");
            IPerson p3 = new Person("Person 3");

            this.CurrentPerson = p1;

            comboBox1.Items.Add(p1);
            comboBox1.Items.Add(p2);
            comboBox1.Items.Add(p3);
            comboBox1.DataBindings.Add("SelectedItem", this, "CurrentPerson");
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBoxEdit1.DataBindings[0].WriteValue();
        }

        private IPerson _currentPerson;
        public IPerson CurrentPerson
        {
            get { return _currentPerson; }
            set { _currentPerson = value; }
        }   
    }


Now... If you run the program, and try to change the selected item, you will see my problem. The selected item is changed back to what it was!! (Originally "Person 1", try to change it to "Person 2" and it jumps back to "Person 1").

I have found out this much:
- If you remove the interface from the project, and just use the Person class directly, there is no problem.
- If you change the IPerson interface and make it an IPerson class instead, it all works just great.
- If you change the CurrentPerson property to be a Person instead of an IPerson, it also solves my problem.
- If you step in debug mode, from the moment the "comboBoxEdit1_SelectedIndexChanged" is called, you will see that when the binding is supposed to set the value of CurrentPerson, it enters the get method instead! Why?? If CurrentPerson is a Person (not IPerson), the binding enters the set method as expected.

What am I doing wrong, and how can I solve it? Any workarounds?

PS: None of the solutions above solves my problem, since I have this problem in a large application where I have a lot of properties that are of interface-types, and that should be bound to different controls. This example just illustrates my problem.


Answers (1)