I'm trying to set the initial selected value (or default value) of a combo box. When the user selects an incident record, the incident status from the incident record is supposed to show up as the default selection of the combo box, instead of the blank selection that appears at the top of the combo box list. I've tried:
Two-way binding the combo box selected value to a property in my model and then setting the property as the incident data is loaded,
Two-way binding the combo box selected index to a property in my model and then setting the property as the incident data is loaded,
Two-way binding the both the combo box selected index and value to a properties in my model and then setting the properties as the incident data is loaded,
but no matter what I tried, the combo box still shows as it would initially with the blank line for a default showing on top of the list.
Here is the XAML for the ComboBox:
<ComboBox Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
IsEnabled="{qc:Binding '!$P', P={Binding Path=Form104CModel.IsPartINA}}"
HorizontalAlignment="Left"
DisplayMemberPath="Description"
SelectedValuePath="Code"
SelectedValue="{Binding Form104CModel.IncidentStatus, Mode=TwoWay}"
SelectedIndex="{Binding Form104CModel.IncidentStatusSelectedIndex, Mode=TwoWay}"
Margin="2,0,2,0" Name="cmbxIncidentStatus"
FontSize="11" FontWeight="Bold" Width="180">
<ComboBox.ItemsSource>
<CompositeCollection>
<domain:Enumeration Code="" Description="" />
<CollectionContainer
Collection="{Binding Source={x:Static infraData:CodeCache.ClearedException}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
Here are the properties in the model to which the selected index and selected value are bound:
private int incidentStatusSelectedIndex;
public int IncidentStatusSelectedIndex
{
get
{
return this.incidentStatusSelectedIndex;
}
set
{
if (this.incidentStatusSelectedIndex != value)
{
this.incidentStatusSelectedIndex = value;
}
}
}
private string incidentStatus;
[CustomValidation(typeof(Form104CModel), "ValidateIncidentStatus")]
public string IncidentStatus
{
get
{
return this.incidentStatus;
}
set
{
if (this.incidentStatus != value)
{
this.ValidateProperty("IncidentStatus", value);
this.incidentStatus = value;
this.RaisePropertyChanged("IncidentStatus");
}
}
}
And here is the code (from the ViewModel) that sets the value of the incident status.
switch (rr.Incident.IncidentStatus)
{
case "A":
this.Form104CModel.IncidentStatus = "CLEARED BY ARREST";
this.Form104CModel.IncidentStatusSelectedIndex = 1;
break;
case "D":
this.Form104CModel.IncidentStatus = "DEATH OF OFFENDER";
this.Form104CModel.IncidentStatusSelectedIndex = 2;
break;
case "E":
this.Form104CModel.IncidentStatus = "EXTRADITION DECLINED";
this.Form104CModel.IncidentStatusSelectedIndex = 3;
break;
case "J":
this.Form104CModel.IncidentStatus = "JUVENILE, NO CUSTODY";
this.Form104CModel.IncidentStatusSelectedIndex = 4;
break;
case "O":
this.Form104CModel.IncidentStatus = "OPEN";
this.Form104CModel.IncidentStatusSelectedIndex = 5;
break;
case "P":
this.Form104CModel.IncidentStatus = "PROSECUTION DECLINED";
this.Form104CModel.IncidentStatusSelectedIndex = 6;
break;
case "R":
this.Form104CModel.IncidentStatus = "REFUSED TO COOPERATE";
this.Form104CModel.IncidentStatusSelectedIndex = 7;
break;
case "U":
this.Form104CModel.IncidentStatus = "UNFOUNDED";
this.Form104CModel.IncidentStatusSelectedIndex = 8;
break;
}
The only thing that I can think of now is that the ComboxBox.ItemsSource is somehow thwarting my efforts to select a default based on the incident status field.
Any suggestions would be much appreciated.