Hi,
In the sample code I am providing, I have assigned both the dataContext and dataSource to the grid in the MainWindow() constructor (the way I need it) and it is not working.
But if I remove the dataSource assignment in the constructor and move it to the "xalm" file, like <igDP:XamDataGrid x:Name="xamDataGrid1" DataSource="{Binding Data}"/> it works. What is the difference between them?
How can I go about dynamically assigning both dataContext and dataSource?
Hi Viswanath -
The reason the DataSource assignment in the window's constructor is not working is because at the time the code does the assignment, datatablemodel.Data is null.
So when the user subsequently presses one of the buttons on the form to choose which data to use and the DataTableViewModel's Data property is updated which correctly raises a PropertyChanged event, there is nothing listening for that event (since the window constructor code did a straight assignment of the grid's DataSource property) and as a result the grid's DataSource Property is not updated.
What the constructor code should be doing instead is binding the grid's DataSource property to the DataTableViewModel's Data property. When this is done the framework's binding infrastructure will automatically listen for the PropertyChanged event on the DataTableViewModel's Data property and the grid's DataSource property will be updated when it changes.
So this line in the window's constructor:
xamDataGrid1.DataSource = datatablemodel.Data;
should be replaced with this line:
xamDataGrid1.SetBinding(XamDataGrid.DataSourceProperty, new Binding { Source = datatablemodel, Path = new PropertyPath("Data"), Mode = BindingMode.OneWay});
The reason it was working for you in the XAML case is that the XAML code (i.e., DataSource="{Binding Data}") Is doing a binding as I'm recommending above.
Hope that helps.
Joe