2012-04-01 2 views
0

여기 벽을 오르기 시작합니다. 콤보 상자에 항목 목록이 채워져 있는데, 어떤 항목이 선택되었는지 알아 내려고합니다. 나는 또한 콤보 상자를 자동으로 목록의 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"); 
      } 
     } 
    } 
+0

DataContext를 설정하고 있습니까? 그렇다면 무엇을해야할까요? – Phil

+0

보기 (사용자 정의 컨트롤)의 datacontext는 위의 코드가 상주하는 viewmodel로 설정됩니다. 콤보 상자의 실제 항목 목록에 대한 바인딩이 작동하므로 이론적으로 다른 두 바인딩도 작동해야합니다. – benjgorman

+0

그 때 일해야한다. SelectedItem 및 SelectedIndex 중 하나만 필요합니다. – Phil

답변

2
  1. 바인딩에서 사용하기를 들어, GroupIndexSelectedGroup 속성은 public해야합니다.
  2. SelectedItemSelectedIndex을 동시에 설정하지 마십시오.
  3. SelectedItem으로 설정하려면 정확한 항목 (SelectedGroup)을 Groups에 포함시켜야합니다.
+0

잘 찾았습니다. SelectedGroup에도 동일하게 적용됩니다. – Phil

+0

젠장, 나는 그게 어리석은 걸 알았다. 감사! – benjgorman

+0

@Phil은 스크롤 바를 발견하지 못했습니다 :-) – Clemens

관련 문제