I am trying to bind ObservableCollection of T to DataGridComboBoxColumn of DataGrid.
DataGrid definition is :
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Model, IsAsync=True}">
<DataGrid.Columns>
<DataGridTextColumn Header="Column Entry" IsReadOnly="True" Binding="{Binding ColumnName}"/>
<DataGridComboBoxColumn Header="Road Type" ItemsSource="{Binding RoadTypes}"/>
</DataGrid.Columns>
</DataGrid>
This is ViewModel and Model
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var viewModel = new ViewModel();
DataContext = viewModel;
}
}
public class ViewModel : ViewModelBase
{
private ObservableCollection<Model> _model;
public ViewModel()
{
var list = new List<Model>();
var roadTypes = new ObservableCollection<RoadType>
{
new RoadType
{
Code = 1,
Id = 1,
Name = "Name1"
},
new RoadType
{
Code = 1,
Id = 1,
Name = "Name1"
}
};
Model = new ObservableCollection<Model>
{
new Model
{
ColumnName = "Col1",
RoadTypes = roadTypes
},
new Model
{
ColumnName = "Col1",
RoadTypes = roadTypes
}
};
}
public ObservableCollection<Model> Model
{
get { return _model; }
set
{
_model = value;
RaisePropertyChanged(() => Model);
}
}
}
public class RoadType
{
public int Id { get; set; }
public int Code { get; set; }
public string Name { get; set; }
}
public class Model : ObservableObject
{
private ObservableCollection<RoadType> _roadTypes;
public string ColumnName { get; set; }
public ObservableCollection<RoadType> RoadTypes
{
get { return _roadTypes; }
set
{
_roadTypes = value;
RaisePropertyChanged(() => RoadTypes);
}
}
}
DataGrid displays text column as well but it doesn't display ComboBox values.
What's wrong?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…