2013-10-24 5 views
0

내 뷰 모델 클래스 표시되지콤보 상자는 항목을 여기에

이제
namespace ERP_Lite_Trial.ViewModels 
{ 
    public class GroupsViewModel : INotifyPropertyChanged 
    { 
     public GroupsViewModel() 
     { 
      using (DBEntities db = new DBEntities()) 
      { 
       var groups = (from g in db.Groups 
           select g.Name).ToList(); 

       this.GroupName = groups; 

       var correspondingEffects = (from g in db.Groups 
              select g.Type_Effect.Name).ToList(); 

       this.EffectCorrespondingToGroup = correspondingEffects; 
      } 
    } 

     private List<string> _groupName; 
     public List<string> GroupName 
     { 
      get 
      { 
       return _groupName; 
      } 
      set 
      { 
       _groupName = value; 
       OnPropertyChanged("GroupName"); 
      } 
     } 

     private List<string> _effectCorrespondingToGroup; 
     public List<string> EffectCorrespondingToGroup 
     { 
      get 
      { 
       return _effectCorrespondingToGroup; 
      } 
      set 
      { 
       _effectCorrespondingToGroup = value; 
       OnPropertyChanged("EffectCorrespondingToGroup"); 
      } 
     } 

     public void OnPropertyChanged(string PropertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(PropertyName)); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
    } 
} 

난 당신이 두 경우 표시됩니다 : 위의 작동 음

<ComboBox x:Name="cbUnder" ItemsSource="{Binding Path=GroupName}" IsEditable="True" 
      Grid.Column="1" Grid.ColumnSpan="4" Grid.Row="3" /> 

:

사례 1를 나는 내 데이터베이스에서 모든 그룹 이름을 얻으며 comboBox의 항목으로 올바르게 표시됩니다. 그러나 이것은 내가 원하는 것이 아닙니다. 이 콤보 박스에 두 개의 열을 표시하고 싶습니다.

사례 2는 예상대로 작동하지

<ComboBox x:Name="cbUnder" IsEditable="True" Grid.Column="1" Grid.ColumnSpan="4" Grid.Row="3"> 
    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding Path=GroupName}" Width="100"/> 
       <TextBlock Text="|" Width="10" /> 
       <TextBlock Text="{Binding Path=EffectCorrespondingToGroup}" Width="100"/> 
      </StackPanel> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox> 

나는이 경우 오류를하지 않았다하지만 내 콤보는 모든 항목을 표시되지 않습니다 (나에 의해 수행 바보 같은 실수가있을 수 있습니다).

답변

4

코드는 하나의 목록에서 서로 관련이 있으므로 현재 두 개의 목록에있는 정보를 만들어야합니다. 두 개의 별도 목록으로는 서로를 연관시킬 수있는 방법이 없습니다.

먼저 DB 쿼리를 변경하여 정보를 개체 목록으로 반환하십시오.

private List<GroupAndEffect> _groupAndEffects; 
    public List<GroupAndEffect> GroupsAndEffects 
    { 
     get 
     { 
      return _groupAndEffects; 
     } 
     set 
     { 
      _groupAndEffects = value; 
      OnPropertyChanged("GroupsAndEffects"); 
     } 
    } 

GroupAndEffect 클래스

public class GroupAndEffect 
{ 
    public string GroupName; 
    public string EffectCorrespondingToGroup; 
} 

업데이트 사례 2를 요구하는 :

 using (DBEntities db = new DBEntities()) 
     { 
      GroupsAndEffects = (from g in db.Groups 
          select new GroupAndEffect 
          { 
          GroupName = g.Name 
          EffectCorrespondingToGroup = g.Type_Effect.Name 
          }).ToList(); 
     } 

var에 그룹 대신 문자열 목록의 객체의 목록이 될 필요가

<ComboBox x:Name="cbUnder" ItemsSource="{Binding GroupsAndEffects}" IsEditable="True" Grid.Column="1" Grid.ColumnSpan="4" Grid.Row="3"> 
<ComboBox.ItemTemplate> 
    <DataTemplate> 
     <StackPanel Orientation="Horizontal"> 
      <TextBlock Text="{Binding GroupName}"/> 
      <TextBlock Text="|" Width="10" /> 
      <TextBlock Text="{Binding EffectCorrespondingToGroup}" Grid.Column="1" /> 
     </StackPanel> 
    </DataTemplate> 
</ComboBox.ItemTemplate></ComboBox> 
+0

매우 정답을 주셔서 감사합니다. 당신의 대답에 문제가 있습니다. 나는 당신의 답을 정확히 따라 왔지만 이제는 모든 항목에 텍스트 대신 파이프 기호 (|)가 표시됩니다. 항목 중 하나를 선택하면 GroupAndEffect가 표시됩니다. – Khushi

+0

음, 이것들은 모두 코드 조각입니다. 최대한 많이 들어가려고 노력했습니다. 이 코드는 전체 코드가 어떻게 작동 하는지를 보여주는 유용한 튜토리얼입니다. http://code.msdn.microsoft.com/windowsdesktop/Best-ComboBox-Tutorial-5cc27f82#content – JnJnBoo

+0

문제점을 발견했습니다. 그러나 그것을 해결하는 방법을 모른다. GroupAndEffect 클래스의 ToString()을 재정의 (override)해야합니다. 아래에 언급 된 msdn aricle에서 찾았습니다. http://msdn.microsoft.com/en-us/library/ms742521.aspx – Khushi

관련 문제