I use a ComboBox which is bound to a List<> of Entities. How can I add a "Not selected" entry to the combobox? Adding null to the list results in empty combobox.
A:
You should use an empty string or other unique text pattern instead of null.
And then You can handle the Format event of the Combobox to intercept the <empty>
and display an alternate text.
private void comboBox1_Format(object sender, ListControlConvertEventArgs e)
{
e.Value = FormatForCombobox(e.ListItem);
}
private string FormatForCombobox(object value)
{
string v = (string) value;
if (v == string.Empty)
v = "<no Selection>";
return v;
}
Henk Holterman
2009-09-07 10:55:19
I can't add anything to the combobox because it is bound to the list of entities.
wRAR
2009-09-07 11:18:38
You can still add an event when it's bound.
Henk Holterman
2009-09-07 11:20:17
It won't help because I have nothing special to format.
wRAR
2009-09-07 11:34:50
You have to add something special first...
Henk Holterman
2009-09-07 11:51:49
I can't add anything to the combobox because it is bound to the list of entities.
wRAR
2009-09-07 11:56:07
You add a special item to the List<> and an eventhandler to the Combobox.
Henk Holterman
2009-09-07 12:00:40
That special item should be entity object. So how do I create a fake entity object? What entity key should it have?
wRAR
2009-09-07 12:48:59
Yes, try to create a special entity, the Key should be an recognizable (invalid) value. If that doesn't work you may need to copy entities to the .Items, and again use Format
Henk Holterman
2009-09-07 13:01:08
Could you possibly extend the combo box class? The binding happens behind the scenes, I wonder if you could override those methods that populate it.. Might be a bit difficult. I wouldn't recommend creating an empty entity here and there... my 2 cents.
lb
2010-02-03 09:39:20