2017-12-30 84 views
0

컬렉션이있는 뷰 모델이 있으며 각 항목에는 키 컬렉션이 있습니다. DataGridComboBoxColumn을 만들어 각 항목의 키 드롭 다운 목록을 표시하려고합니다. 나는 비슷한 질문을 보았지만 그 어떤 대답도 나를 도왔다. 응용 프로그램을 실행할 때 모든 콤보 상자가 비어 있습니다. 여기 내 XAML입니다 :DataGridComboBoxColumn을 뷰 모델 컬렉션에 바인딩 할 수 없습니다.

<Window 
    x:Class="TestDataGridCombobox.MyWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
    <DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False"> 
     <DataGrid.Columns> 
      <DataGridTextColumn Binding="{Binding Name}"/> 
      <DataGridComboBoxColumn ItemsSource="{Binding Path=Keys}" SelectedValueBinding="{Binding Path=SelectedKey}"/> 
     </DataGrid.Columns> 
    </DataGrid> 
</Window> 

그리고 여기 내보기 모델입니다 :

using System.Collections.ObjectModel; 
using System.ComponentModel; 

namespace TestDataGridCombobox 
{ 
    public class MyViewModel : INotifyPropertyChanged 
    { 
     public MyViewModel() 
     { 
      Items.Add(new MyItem { Name = "Item1" }); 
      Items.Add(new MyItem { Name = "Item2" }); 
      Items.Add(new MyItem { Name = "Item3" }); 
     } 

     private ObservableCollection<MyItem> items = new ObservableCollection<MyItem>();   
     public ObservableCollection<MyItem> Items 
     { 
      get { return items; } 
      set 
      { 
       if (items == value) 
        return; 
       items = value; 
       OnPropertyChanged("Items"); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     private void OnPropertyChanged(string property) 
     { 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property)); 
     } 
    } 

    public class MyItem : INotifyPropertyChanged 
    { 
     public MyItem() 
     { 
      Keys.Add("Key1"); 
      Keys.Add("Key2"); 
      Keys.Add("Key3"); 
      SelectedKey = "Key1"; 
     } 

     private string name; 
     public string Name 
     { 
      get { return name; } 
      set 
      { 
       if (name == value) 
        return; 
       name = value; 
       OnPropertyChanged("Name"); 
      } 
     } 

     private string selectedKey; 
     public string SelectedKey 
     { 
      get { return selectedKey; } 
      set 
      { 
       if (selectedKey == value) 
        return; 
       selectedKey = value; 
       OnPropertyChanged("SelectedKey"); 
      } 
     } 

     private ObservableCollection<string> keys = new ObservableCollection<string>(); 
     public ObservableCollection<string> Keys 
     { 
      get { return keys; } 
      set 
      { 
       if (keys == value) 
        return; 
       keys = value; 
       OnPropertyChanged("Keys"); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     private void OnPropertyChanged(string property) 
     { 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property)); 
     } 
    } 
} 

그리고 뷰 모델은 다음과 같은 창에 바인딩 :

public partial class MyWindow : Window 
{ 
    public MyWindow() 
    { 
     InitializeComponent(); 
     DataContext = new MyViewModel(); 
    } 
} 

가 나는 템플릿 열을 사용할 수 있습니다 ,하지만이 특별한 예제가 작동하지 않는 이유에 관심이 있습니다. 코드에 문제가 있습니까? 아니면 DataGridComboBoxColumn에 대한 제한이 있습니까?

답변

1

DataGridComboBoxColumn에는 제한 사항이 있습니까?

예, 몇 가지 제한 사항이 있습니다. 당신은 비고에게 DataGridComboBoxColumn Class documentation에서 섹션을 읽어을 찾을 수 있습니다

    :

    드롭 다운 목록을 채우려면을 먼저 다음 옵션 중 하나를 사용하여 콤보 상자의 ItemsSource 속성을 설정

  • 정적 리소스입니다. 자세한 내용은 StaticResource Markup Extension을 참조하십시오.
  • x : 정적 코드 엔티티. 자세한 내용은 x:Static Markup Extension을 참조하십시오.
  • 유형의 인라인 모음입니다. 당신이 쓴대로 - -

는 해결의 많은 예를 들어,이 문제를 해결하기 위해이 있습니다 당신이 DataGridTemplateColumn를 사용할 수 있습니다.

도움이되기를 바랍니다.