Lookup a control in a GridTemplateColumn.EditTemplate

Hi,

I have a GridTemplateColumn which contains custom controls in a CellTemplate and an EditTemplate. At runtime, how do I obtain a reference to the controls in the current cell in C#? I'm trying to do this in the CurrentCellBeginEdit handler so that I can setup the controls for editing. I tried to look them up in the visual tree based on the class and the x:Name but it always returns null. I suspect there must be multiple instances of the controls when using GridTemplateColumn so I have to lookup the ones associated with the current cell.

Regards,

Sam



3 Replies

JG Jai Ganesh S Syncfusion Team April 17, 2017 12:45 PM UTC

Hi Sam, 
 
You can get the control inside the edit template of GridTemplateColumn by passing the control name like below,  
private async void Sfdatagrid_CurrentCellBeginEdit(object sender, CurrentCellBeginEditEventArgs e) 
{ 
    await this.Dispatcher.BeginInvoke(new Action(() => 
    { 
    int recordIndex = this.sfdatagrid.ResolveToRecordIndex(e.RowColumnIndex.RowIndex); 
 
    var rowGenerator = this.sfdatagrid.GetRowGenerator() as RowGenerator; 
             
        var columnElement= this.sfdatagrid.SelectionController.CurrentCellManager.CurrentCell.ColumnElement as GridCell; 
 
        var rowData = columnElement.DataContext; 
 
        var dataColumn = columnElement.ColumnBase as DataColumn; 
                 
 
        if (dataColumn.Renderer.GetType() == typeof(GridCellTemplateRenderer) && dataColumn.GridColumn.MappingName=="EmployeeId") 
        { 
                    
            DataTemplate dataTemplate = null; 
            ComboBox comboBox = null; 
            ContentPresenter contentPresenter = this.FindVisualChild<ContentPresenter>(columnElement); 
 
            dataTemplate = contentPresenter.ContentTemplateSelector.SelectTemplate(rowData, contentPresenter); 
            comboBox = (ComboBox)dataTemplate.FindName("combobox", contentPresenter); 
                             
        } 
 
                 
    }), System.Windows.Threading.DispatcherPriority.ApplicationIdle); 
} 
 
 
In the above sample, we have loaded the Combobox inside the EditTemplate of EmployeeId column and you can get it by using the CurrentCellBeginEdit event, 
 
Regards, 
Jai Ganesh S 



SC Sam Chan April 18, 2017 11:13 AM UTC

Thank you Jai,

I was able to adapt your sample code to achieve what I wanted to do. I'm glad that I asked the question instead of spending more time trying to figure it out myself because the solution is not that trivial. I would have never thought it needed to be done in a Dispatcher.

Regards,

Sam




MK Muthukumar Kalyanasundaram Syncfusion Team April 20, 2017 08:47 AM UTC

Hi Sam, 
 
Thanks for the update. Please let us know if you need any other assistance. 
 
Regards, 
Muthukumar K 


Loader.
Up arrow icon