I am using the DataGridComboBoxColumn
, and if I set the datacontext to the Window in XAML means the ItemsSource
loaded fine in combobox
column. If I set the datacontext to the window in code behind in MainWindow
constructor after the InitializeComponent()
, Itemssource
not loaded in the combobox column.
Could you please share any suggestion on this ?
Code :
<Window.DataContext>
<local:OrderInfoRepositiory/>
</Window.DataContext>
<DataGrid Name="dataGrid1"
Width="308"
Height="200"
HorizontalAlignment="Left"
VerticalAlignment="Top"
AutoGenerateColumns="False"
ItemsSource="{Binding OrderInfoCollection}">
<DataGrid.Columns>
<DataGridComboBoxColumn x:Name="ComboBoxColumn"
Header="Position"
ItemsSource="{Binding ComboItemSource}"
/>
<DataGridTextColumn Binding="{Binding OrderID}" Header="Name" />
</DataGrid.Columns>
</DataGrid>
OrderInfoRepository definition:
public class OrderInfoRepositiory : INotifyPropertyChanged
{
private ObservableCollection<string> comboitemSource;
public ObservableCollection<string> ComboItemSource
{
get
{
return comboitemSource;
}
set
{
comboitemSource = value;
RaisePropertyChanged("ComboItemSource");
}
}
ObservableCollection<OrderInfo> orderCollection;
public ObservableCollection<OrderInfo> OrderInfoCollection
{
get { return orderCollection; }
set { orderCollection = value; RaisePropertyChanged("OrderInfoCollection"); }
}
public OrderInfoRepositiory()
{
orderCollection = new ObservableCollection<OrderInfo>();
OrderInfoCollection = GenerateOrders();
ComboItemSource = new ObservableCollection<string>();
ComboItemSource.Add("Germany");
ComboItemSource.Add("Mexico");
ComboItemSource.Add("Sweden");
ComboItemSource.Add("France");
ComboItemSource.Add("Spain");
ComboItemSource.Add("Canada");
}
}