여기 벽을 오르기 시작합니다. 콤보 상자에 항목 목록이 채워져 있는데, 어떤 항목이 선택되었는지 알아 내려고합니다. 나는 또한 콤보 상자를 자동으로 목록의 0 번째 항목을 선택하도록 설정하려고 시도했다. 하지만 그 중 하나를 작동하지 않습니다, GroupIndex 및 SelectedGroup 바인딩 식 경로 오류가 있어요. 그러나 나는 그 문제가 무엇인지 알 수 없다.콤보 상자의 SelectedItem 또는 SelectedIndex를 가져올 수 없습니다.
콤보 상자 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");
}
}
}
DataContext를 설정하고 있습니까? 그렇다면 무엇을해야할까요? – Phil
보기 (사용자 정의 컨트롤)의 datacontext는 위의 코드가 상주하는 viewmodel로 설정됩니다. 콤보 상자의 실제 항목 목록에 대한 바인딩이 작동하므로 이론적으로 다른 두 바인딩도 작동해야합니다. – benjgorman
그 때 일해야한다. SelectedItem 및 SelectedIndex 중 하나만 필요합니다. – Phil