S
Steve K.
I'm surprised that I'm having trouble with this, I suspect that I'm having a
major brain fart and expecting something to work a way it doesn't.
In my example, I have a List<> of custom objects bound to a ComboBox.
I then have a single instance of the custom object which I have bound to the
SelectedItem property.
I'm expecting:
1) When the user changes the selection from the drop down the single, bound
instance will reference the selected item from the bound collection.
2) When binding the control to the single instance, it should select the
item in the list that matches (if there is one)
Here is a very simple example. I didn't include all the WinForm stuff, as
long as you have a ComboBox names comboBox1 it should be cut and paste.
<code>
List<SomeObject> _objects;
SomeObject _object;
public Form1()
{
InitializeComponent();
_objects = new List<SomeObject>();
_object = new SomeObject("C", 3);
_objects.Add(new SomeObject("A", 1));
_objects.Add(new SomeObject("B", 2));
_objects.Add(new SomeObject("C", 3));
_objects.Add(new SomeObject("D", 4));
comboBox1.DataSource = _objects;
comboBox1.ValueMember = "Value";
comboBox1.DisplayMember = "Display";
comboBox1.DataBindings.Add(new Binding("SelectedItem", _object,
"Value"));
comboBox1.SelectedIndexChanged += delegate(object sender, EventArgs e)
{
MessageBox.Show(string.Format("_object.Value = {0}\n_object.Display
= {1}", _object.Value, _object.Display));
};
}
public class SomeObject
{
private string _display;
private int _value;
public SomeObject(string display, int value)
{
_display = display;
_value = value;
}
public string Display
{
get { return _display; }
set { _display = value; }
}
public int Value
{
get { return _value; }
set { _value = value; }
}
}
</code>
If I change the code to use SelectedValue then it will select the correct
item in the list, however making a new selection in the list does not update
the _object reference OR it's Value property.
If anyone can straighten me out on this I'd really appreciate it, I'm going
in circles over here! ;0)
-Steve
major brain fart and expecting something to work a way it doesn't.
In my example, I have a List<> of custom objects bound to a ComboBox.
I then have a single instance of the custom object which I have bound to the
SelectedItem property.
I'm expecting:
1) When the user changes the selection from the drop down the single, bound
instance will reference the selected item from the bound collection.
2) When binding the control to the single instance, it should select the
item in the list that matches (if there is one)
Here is a very simple example. I didn't include all the WinForm stuff, as
long as you have a ComboBox names comboBox1 it should be cut and paste.
<code>
List<SomeObject> _objects;
SomeObject _object;
public Form1()
{
InitializeComponent();
_objects = new List<SomeObject>();
_object = new SomeObject("C", 3);
_objects.Add(new SomeObject("A", 1));
_objects.Add(new SomeObject("B", 2));
_objects.Add(new SomeObject("C", 3));
_objects.Add(new SomeObject("D", 4));
comboBox1.DataSource = _objects;
comboBox1.ValueMember = "Value";
comboBox1.DisplayMember = "Display";
comboBox1.DataBindings.Add(new Binding("SelectedItem", _object,
"Value"));
comboBox1.SelectedIndexChanged += delegate(object sender, EventArgs e)
{
MessageBox.Show(string.Format("_object.Value = {0}\n_object.Display
= {1}", _object.Value, _object.Display));
};
}
public class SomeObject
{
private string _display;
private int _value;
public SomeObject(string display, int value)
{
_display = display;
_value = value;
}
public string Display
{
get { return _display; }
set { _display = value; }
}
public int Value
{
get { return _value; }
set { _value = value; }
}
}
</code>
If I change the code to use SelectedValue then it will select the correct
item in the list, however making a new selection in the list does not update
the _object reference OR it's Value property.
If anyone can straighten me out on this I'd really appreciate it, I'm going
in circles over here! ;0)
-Steve