How can I load combo box's data when it has been defined in DataTemplate codebehind ?
- by Naseem
Hi,
In my silverlight application,I need to have dynamic columns in my DataGrid . So I had to create all the columns and their DataTemplate dynamically .When user wants to edit the column , a combo box will be displayed which has different values based on selected column.
For creating each column I have wrote :
foreach (var itemFilter in ProductFilterCollection)
{
DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
templateColumn.Header = itemFilter.Description.ToString();
templateColumn.CellTemplate = CreateCellTemplate(typeof(TextBlock), itemFilter.Description.ToString());
templateColumn.CellEditingTemplate = CreateEditingTemplate(typeof(ComboBox), itemFilter.Description.ToString());
grdTest.Columns.Add(templateColumn);
}
Here is the code for creating DataTemplate dynamically.
public DataTemplate CreateCellTemplate(Type type, string strBinding)
{
return (DataTemplate)XamlReader.Load(@"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007""> <" + type.Name + @" Text=""{Binding " + strBinding + @"}""/> </DataTemplate>");
}
public DataTemplate CreateEditingTemplate(Type type, string strBinding)
{
return (DataTemplate)XamlReader.Load(@"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007""> <" + type.Name + @" Loaded=""ddlTest_Loaded"" Tag=""{Binding " + strBinding + @"}""/> </DataTemplate>");
}
I need to use Loaded="ddlTest_Loaded" in CreateEditingTemplate() ,in order to load the combo box . However it causes exception . When I remove Loaded event it's fine however the combo box is empty. How can I load the combo box when I define it in DataTemplate codebehind.