I am having trouble with a telerik combobox in silverlight
I have defined it like this,
<telerik:RadComboBox SelectedItem="{Binding Organisation, Mode=TwoWay}" ItemsSource="{Binding Organisations}" DisplayMemberPath="Name" />
And the SelectedItem property bound to looks like this,
[Required(ErrorMessage = "The organisation is required.")]
public OrganisationEntity Organisation
{
get
{
return utilityOwnerOrganisation;
}
set
{
utilityOwnerOrganisation = value;
RaisePropertyChanged(this, x => x.UtilityOwnerOrganisation);
}
}
The OrganisationEntity is like this,
public class OrganisationEntity
{
public string Name { get; set; }
public int OrganisationId { get; set; }
}
The problem arises if the list Organisations have two organisations in it defined like this,
new OrganisationEntity() { Name = "Wellington City Council", OrganisationId = 34 }
new OrganisationEntity() { Name = "Wellington City Council", OrganisationId = 31 }
If I have two items with the same Name in the list and set the Organisation property (from the viewmodel) to be the item with OrganisationId = 31 the setter is called from the binding engine and the other item gets selected.
The reason is because of DisplayMemberPath being set to Name. It is treating the Name as being a key and assuming that it is unique in the combobox, but it isn’t.
If I take out the DisplayMemberPath it works, but the combobox then displays the wrong thing.
If I take out the DisplayMemberPath and define a ToString method to return the Name property in the OrganisationEntity the same thing occurs.
Try to remove the DisplayMemberPath and use a DataTemplate to show what you want instead: