There is an ItemViewModel of the following form:

public ItemViewModel(string name) { ComboBoxContent = new ObservableCollection<string>(); ComboBoxContent.Add("1"); ComboBoxContent.Add("2"); ComboBoxContent.Add("3"); Name = name; } public ObservableCollection<string> ComboBoxContent { get; set; } public string Name { get; set; } 

And there is an ItemsViewModel:

 public ItemsViewModel() { Items = new ObservableCollection<ItemViewModel>(); Items.Add(new ItemViewModel("name1")); Items.Add(new ItemViewModel("name2")); Items.Add(new ItemViewModel("name3")); } public ObservableCollection<ItemViewModel> Items { get; set; } 

It is necessary to fill DataGrid with values ​​from ItemViewModel. I will give a schematic code as I imagine it:

  <DataGrid ItemsSource="{Binding Items}"> <DataGrid.Columns> <DataGridTextColumn Header="Name" Binding="{Binding Name}"></DataGridTextColumn> <DataGridComboBoxColumn Header="ComboBox" ItemsSource="{Binding ComboBoxContent}"></DataGridComboBoxColumn> </DataGrid.Columns> </DataGrid> 

How should I do this correctly?

  • and what doesn't work? - Gardes
  • The fact that with such a call the property is searched not inside the Items collection, but in the DataContext - Oblfakir
  • one
    the markup is correct, maybe you don’t bind to Items at all, look in the output window when compiling - Gardes
  • Cannot find governing FrameworkElement or FrameworkContentElement for target element. Yes, there is an error. Name is attached correctly and displayed, but with combobox binding does not work. - Oblfakir

1 answer 1

Use DataGridTemplateColumn to use a ComboBox inside a cell:

  <DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="Name" Binding="{Binding Name}"></DataGridTextColumn> <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox ItemsSource="{Binding ComboBoxContent}"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid>