2011-03-09 91 views
1

預先感謝您的幫助。 我不得不從代碼中爲celleditingtemplate(datatemplate)中的usercontrol設置綁定。即我有一個帶有2個datatemplatecolumns的網格,它有cellediting模板指向相同的數據模板。我需要根據用戶正在編輯的單元格動態更改celleditingtemplate的綁定。 任何幫助將不勝感激。CellEditingTemplate中的動態綁定

謝謝

這裏是示例代碼。 這是在不同的資源文件

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

這是我需要設置綁定頁面上後面的代碼。

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);         
    }   
} 

回答

0

找到它。

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

感謝