컬렉션이있는 뷰 모델이 있으며 각 항목에는 키 컬렉션이 있습니다. 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에 대한 제한이 있습니까?