I've got a ComboBox in a WPF app that I populate from a lookup table. The requirements say that if the user has chosen to create a new record into a data type, that the ComboBox is to display the text "Select...". That text isn't in the lookup table, so I create a dummy record and insert it into the ObservableCollection used to populate the ComboBox like so:
var newTmpCert = new CertificationType()
{
ID = 0,
CertType = SELECT_DROPDOWN_STRING, //SELECT_DROPDOWN_STRING == "Select..."
Inactive = false
};
CertTypeList.Insert(0, newTmpCert);
CertTypeList is defined like this:
private ObservableCollection<CertificationType> certTypeList;
public ObservableCollection<CertificationType> CertTypeList
{
get { return certTypeList; }
set
{
certTypeList = value;
RaisePropertyChanged();
}
}
The assignment that I've been struggling with to make the ComboBox show "Select..." is this:
Course new_record = new Course();
new_record.CertificationLevel = "Re-certification";
Course = new_record;
CertificationTypeID = 0;
And the XAML for the ComboBox is this:
<ComboBox Width="200"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding CertTypeList}"
DisplayMemberPath="CertType"
SelectedValuePath="ID"
SelectedValue="{Binding CertificationTypeID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
Every time I test the app and select to create a new Certification record, the ComboBox is always blank. So, I thought I'd try something different, and assign CertificationTypeID to 2 (there's a third record in that lookup table), that works perfectly.
Why does it work? The ObservableCollection should raise a propertychanged event. The ComboBox is not bound to the lookup table in the database, it is bound to the ObservableCollection. Why does it work, if I specify an existing record in the lookup table, but doesn't work if I assign 0 to CertificationTypeID in the viewmodel?