No item is selected on addition of row because ItemsSource
and SelectedValue
refers to completely different instances.
I don't know why you are always returning new instance every time Sex.Male
and Sex.Female
is accessed. So, when ItemsSource gets set, new instance of Male and Female gets added and when you add row, new instance of Male/Female is returned which is not there in ItemSource and hence no item was selected.
Either set Sex property to already added instance in ComboBox like this:
TestDataGrid.Add(new Person { Name = Guid.NewGuid().ToString(),
Sex = (male ? Sexes.First() : Sexes.Last()) });
OR
Always return same instance from Male/Female:
public class Sex
{
private static Sex male = new Sex(MaleSexId);
public static Sex Male
{
get
{
return male;
}
}
private static Sex female = new Sex(FemaleSexId);
public static Sex Female
{
get
{
return female;
}
}
.....
}
Ideally Sex
should have been Enum, no need of it having a class for it.
public enum Sex
{
Male,
Female
}