I have the following problem:
When my view loads the combobox selected value is empty.
Here is the code for my view:
<UserControl.Resources>
...
<CollectionViewSource x:Key="SpecialtiesSource" Source="{Binding Path=Specialties}" />
...
</UserControl.Resources>
...
<ComboBox Grid.Row="11" Grid.Column="2" Name="Specialty" Style="{StaticResource ComboBoxItem}"
SelectedIndex="0"
IsEditable="True"
TextSearch.TextPath="Name"
DisplayMemberPath="Name"
ItemsSource="{Binding Source={StaticResource SpecialtiesSource}}"
SelectedItem="{Binding SelectedSpecialty}"
SelectedValuePath="Name">
</ComboBox>
Here is my view model:
private List<Specialty> _specialties;
public List<Specialty> Specialties
{
get
{
return _specialties;
}
set
{
_specialties = value;
NotifyOfPropertyChange(() => Specialties);
}
}
private Specialty _selectedSpecialty;
public Specialty SelectedSpecialty
{
get
{
return _selectedSpecialty;
}
set
{
if (value == null)
{
return;
}
_selectedSpecialty = value;
NotifyOfPropertyChange(() => SelectedSpecialty);
}
}
Model:
public class Specialty : IGeneric
{
public string Code { get; set; }
public string Name { get; set; }
public string TabColour { get; set; }
public string TextColour { get; set; }
public bool? ActiveFlag { get; set; }
}
public interface IGeneric
{
string Name { get; set; }
}
When I am debugging the code I can see that my Specialties object is returned with the correct results, because when I click the drop down menu I can then see the all of the results as I would expect.
However immediately after it has loaded this object, I can see my SelectedSpecialty object then being hit which is null. I am guessing this is because of some underlying interaction between when the ItemsSource is loaded and then attempting to set the value for SelectedItem at index position 0?
Any input or advice on how I can figure out why SelectedItem is being set and how I can stop/set this correctly the the first item of my SpecialtiesSource?
I'm also not 100% confident I'm using the SelectedValuePath correctly.