我开始在这里爬墙。我已经填充了一个包含项目列表的组合框,而我正在尝试找出已选择的项目。我还试图将combox框设置为自动选择列表中的第0项。但是这也行不通,我在GroupIndex和SelectedGroup上都遇到了绑定表达式路径错误。但是我无法弄清楚问题是什么。
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" />
以下是填充组合框的代码。
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");
}
}
}
答案 0 :(得分:2)
GroupIndex
和SelectedGroup
属性必须为public
。SelectedItem
和SelectedIndex
。SelectedItem
,则必须在SelectedGroup
中包含该确切项目Groups
。