I’m beginning to climb the walls here. I’ve filled a combo box with a list of items, and them I’m trying to find out which has been selected. I was also trying to set the combox box to auto selected the 0th item in the list. But that won’t work either, I’m getting a binding expression path error on both GroupIndex and SelectedGroup. However I can’t figure out what the problem is.
Combo Box Xaml:
<ComboBox
Name="GroupComboBox"
SelectedIndex="{Binding Path=GroupIndex}"
SelectedItem="{Binding Path=SelectedGroup}"
DisplayMemberPath="Name"
SelectedValuePath ="Name"
ItemsSource="{Binding Path=Groups}"
Height="38"
HorizontalAlignment="Left"
Margin="159,115,0,0"
VerticalAlignment="Top"
Width="185"
FontSize="24" Text="Select a Group" IsEditable="False" IsReadOnly="False" />
Here is the code for filling the combo box.
void webService_GroupListChanged(string response)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
IList<JSONGroup> listOfGroups = new List<JSONGroup>();
listOfGroups = serializer.Deserialize<List<JSONGroup>>(response);
Application.Current.Dispatcher.BeginInvoke((Action)(() =>
{
Groups = new ObservableCollection<Group>();
foreach(JSONGroup group in listOfGroups)
{
Groups.Add(new Group(group.name, group.id));
}
}));
}
private int groupIndex;
private int GroupIndex
{
get { return this.groupIndex; }
set
{
if (this.groupIndex != value)
{
this.groupIndex = value;
this.OnPropertyChanged("GroupIndex");
}
}
}
private Group selectedGroup;
private Group SelectedGroup
{
get { return this.selectedGroup; }
set
{
if (this.selectedGroup != value)
{
this.selectedGroup = value;
this.OnPropertyChanged("SelectedGroup");
}
}
}
private ObservableCollection<Group> groups;
public ObservableCollection<Group> Groups
{
get { return this.groups; }
set
{
if (this.groups != value)
{
this.groups = value;
this.OnPropertyChanged("Groups");
}
}
}
GroupIndexandSelectedGroupproperties must bepublic.SelectedItemandSelectedIndexat the same time.SelectedItem, that exact item (SelectedGroup) has to be contained inGroups.