-2

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");
        }
}
Elavarasan M
  • 182
  • 1
  • 11

1 Answers1

-1

There is a very small technical difference in setting the DataContext in xaml and the code behind.

When you define it in the xaml's code behind the UI will be initialized first and then the DataContext is initialized while if you define it in the xaml itself the DataContext is initialized while initializing the UI itself.

Unless you are missing the PropertyChanged notification in your DataContext there should not be any visible difference between defining the DataContext in xaml's code behind or xaml itself.

Sandesh
  • 2,966
  • 1
  • 20
  • 34
  • I have used the propertychanged. also by this http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/#comment-82716 one, i am able to acheive my requirement. Is any other way other than this ? – Elavarasan M Oct 21 '15 at 04:19
  • Can you post your class definition of `OrderInfoRepositiory`? – Sandesh Oct 21 '15 at 04:37