Dynamic Binding in CellEditingTemplate

821 views Asked by At

Thanks in advance for the help. I'm having to set the binding for the usercontrol which is in the celleditingtemplate(datatemplate) from the code. i.e. I have a grid with 2 datatemplatecolumns which has celleditingtemplate that points to the same datatemplate. I need to change the binding of the celleditingtemplate dynamically based on which cell the user is editing. Any help will be highly appreciated.

Thanks

Here is the sample code. This is in a separate Resource file

<DataTemplate x:Key="TextCellEditingTemplate">        
          <UserControls:TextControl HorizontalAlignment="Left"/>    
</DataTemplate>

This is the code behind on the page where I need to set the binding.

private void datagrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)        
{            
var row = e.Row;            
var column = e.Column as DataGridTemplateColumn;            
if (column != null)            
  {                
//sets the template based on the dataelement                
column.CellEditingTemplate = UserControlFactory.GetCellEditingTemplate(((row.DataContext) as CollectionData).DataElement);                                 
  }        
}
1

There are 1 answers

0
user652112 On BEST ANSWER

Found it.

private void datagrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
    {
        e.EditingElement.DataContext = CollectionDataToEdit;
    }

Thanks