Reputation: 38209
I have a property ObservableCollection SomeEmployee in my view model class:
private ObservableCollection<Employee> someEmployee=new ObservableCollection<Employee>();
public ObservableCollection<Employee> SomeEmployee
{
get {return someEmployee;}
set {
someEmployee=value;
OnPropertyChanged("SomeEmployee");
}
SomeEmployee is populated by PopulateObject() method
public static IEnumerable<Employee> PopulateObject()
{
IEnumerable<Employee> list = new List<Employee>()
{
new Employee { EmployeeName = "Bob", EmployeeID=1,
ListOfEmployees=new List<Employee>(){
new Employee(){EmployeeName="Gigi"},
new Employee(){EmployeeName="Bacconi"}
new Employee(){EmployeeName="Teodor"} }
},
new Employee { EmployeeName = "John", EmployeeID=2, },
new Employee { EmployeeName = "Adam", EmployeeID=3, },
};
return list;
}
And Employee class has a property ListOfEmployees which I would like to show in DataGridComboBoxColumn of DataGrid:
public class Employee
{
public int? EmployeeID {get; set;}
public string EmployeeName {get; set;}
public List<Employee> ListOfEmployees {get; set;}
And my DataGrid:
<DataGrid ItemsSource="{SomeEmployee}">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="ComboBox" SelectedItemBinding="{Binding EmployeeName}">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="Binding Path=DataContext.ListOfEmployees}"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
However, at first ComboBox is not shown, it is just like simple DataGridTextColumn is shown. At second, there is no data in the cell.
How to populate DataGridComboBoxColumn from property List located inside of ObservableCollection?What am I doing wrong? Any help would be greatly appreciated.
Upvotes: 0
Views: 543
Reputation: 1510
You don't need to set ItemsSource in style, you can do something like:
<DataGrid ItemsSource="{SomeEmployee}">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="ComboBox" SelectedItemBinding=" {Binding EmployeeName}" ItemSource="{Binding EmployeeID}">
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
ListOfEmployees
doesn't exists in your Model, I suppose you want to bind to EmployeeID
Upvotes: -1