We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

Lookup a control in a GridTemplateColumn.EditTemplate

Thread ID:





130022 Apr 16,2017 08:37 AM Apr 20,2017 04:47 AM WPF 3
Tags: SfDataGrid
Sam Chan
Asked On April 16, 2017 08:37 AM


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.



Jai Ganesh S [Syncfusion]
Replied On April 17, 2017 08:45 AM

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, 
Jai Ganesh S 

Sam Chan
Replied On April 18, 2017 07:13 AM

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.



Muthukumar Kalyanasundaram [Syncfusion]
Replied On April 20, 2017 04:47 AM

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


This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.