воскресенье, 1 февраля 2009 г.

Nulls in ComboBox

There is a problem in selecting null values from WPF's ComboBoxes, ListViews and maybe any other standart Selector or ItemsControl.  The problem is this: when you are using collection with null items as an ItemsSource 1) in ComboBox all null items are presented but can't be selected - when you click at the item it behaves like being selected, the popup closes, but you still have an old value selected and no binding happenes;  2) in ListView there is just no null items - they are filtered out.

It seemes that WPF being made for database binding anyway, so all nulls had to be a DBNulls.  So, you still have an option to add blank values in you ItemsControls: just use DBNull.Value.

Should look something like this:



static public IEnumerable<Contractor> Contractors
{
get { return ConditionalGetObjectSet<Contractor>(); }
}
static public IEnumerable<object> ContractorsWithNull
{
get
{
List<object> result = ConditionalGetObjectSet<Contractor>().Cast<object>().ToList();
result.Insert(0, DBNull.Value);
return result;
}
}