I am using the following code which works fine when there is at least one row already created on XamDataGrid UI screen. But i would like to create a dummy row(assuming no rows on UI)at run time by using my data source collection and run the following code. In this scenario, the CellValuePresenter.FromCell() returns null. Please do suggest me a solution.
var comboEditor=(XamComboEditor)(CellValuePresenter.FromCell(((DataRecord)(XamGrid.Records[0])).Cells[field.Name]).Editor)
if (comboEditor != null) {
//...
}
Hello Venkata,
I am curious as to when your "dummy row" is being added to your XamDataGrid and at what point you are calling the code that you have provided. At the moment, I find it likely that at the point that you are calling the CellValuePresenter.FromCell method, that the presenter for that particular cell has not been created yet, and so the CellValuePresenter.FromCell returns null at the time of calling.
When adding a record at runtime, I have found that the CellsInViewChanged event of the XamDataGrid helps with the above. Using that event, I am able to return a CellValuePresenter element from the CellValuePresenter.FromCell method that you have mentioned above. I would recommend trying this on your end to see if the result is the same.
I hope this helps you. Please let me know if you have any other questions or concerns on this matter.
Sincerely,AndrewAssociate Developer
Hello Andrew,
I got your point. As you mentioned i am adding a dummy record into my datasource collection and immediately calling CellValuePresenter.FromCell method. But i do not want to subscribe and wait until that event fires back and write my further logic inside. Is there a way i can add a dummy record programatically and call CellValuePresenter.FromCell method afterwards?? .
I have been looking into a way to be able to programmatically add a record and retrieve the CellValuePresenter of one of the cells in that same event, and I believe I have found a solution for you. Using the Loaded event of the XamDataGrid, and a couple of usages of the Dispatcher.BeginInvoke(new Action(() => { 'your code here' })); method, I was able to produce a situation in which the cells are created after adding the record to the grid. The code for this looks like the following, where "vm.Data" is the underlying bound collection to the XamDataGrid:
Dispatcher.BeginInvoke(new Action(() =>{ XamDataGrid grid = sender as XamDataGrid; vm.Data.Add(new SampleData() { ID = null, Label = string.Empty, Number = null });
Dispatcher.BeginInvoke(new Action(() => { DataRecord record = XDG1.Records[0] as DataRecord; var x = CellValuePresenter.FromCell(record.Cells[0]); }));}));
Using the above code in the Loaded event of the XamDataGrid appeared to work to achieve your requirement on this matter. I have attached a sample project to demonstrate the above. I hope it helps you.
Please let me know if you have any other questions or concerns on this matter.