I have a following XAML:
<ComboBox
Name="groupComboBox"
ItemsSource="{Binding Path=MyServiceMap.Groups}"
DisplayMemberPath="{Binding Name}"/>
In the code behind i set this.DataContext to my viewModel.
private ServiceMap _serviceMap;
public ServiceMap MyServiceMap
{
get
{
return _serviceMap;
}
set
{
_serviceMap = value;
OnPropertyChanged("MyServiceMap");
}
}
My ServiceMap class is
public class ServiceMap
{
//other code
public List<Group> Groups = new List<Group>();
}
and finally:
public class Group
{
public string Name { get; set; }
}
Unfortunately, this is not working. How can i bind combobox to show Group Name?
There are two problems with your code. First is bindings are only work on properties, so the binding couldn’t find the Group field. Change it to a property.
The second one is that the DisplayMemberPath waits for a string not a binding. Change it simply to “Name”.