How to create combobox template column in code behind. RRS feed

  • General discussion

  • Hi,

    I am in serious need of some help on the below issue and already i spent more than 1 day on this.

    I have followed the below post by colin and was able to create a silverlight datagrid with dynamically created columns columns based on number of fields in itemsource and binding the same with dynamic data. So everything happens on runtime here.


    My problem here is i need to create different columns(dropdown, datetime etc) based on the field type from the itemsource. I am able to create different column but unable to bind the source to it. Below is the code i used to create a dynamic combobox column.

    if (propertyType.ToLower() == "choice")
                        return new DataGridTemplateColumn()
                            Header = property,                       
                            CellTemplate = (System.Windows.DataTemplate)System.Windows.Markup.XamlReader.Load(@"<DataTemplate 
                           <ComboBox SelectedValue=""{Binding Path=Data,Converter={StaticResource _rowIndexConverter},ConverterParameter=property,Mode=TwoWay}"">                
                           <ComboBoxItem>" + data1 + "</ComboBoxItem><ComboBoxItem>" + data1 + "</ComboBoxItem>  </ComboBox></DataTemplate>"),



    This combobox does not recognize the itemsource and that is the reason i hardcoded it. When i run this code i am getting "XamlParseException" error. If i remove the convertor from the combobox tag i am not getting any error but the bound value is not shown on the combobox and also if we change selection in combobox the same is not getting updated back to its itemsource.

    So i want to create a combobox template column for the logic given in the above provided link. I doubt that the way i set the converter and converter parameter for the combobox is not right and also no idea why itemsource is also not working.

    Any suggestions would be of Great help. Thanks in advance !!!

    • Edited by Vino Tuesday, July 22, 2014 2:45 PM
    Tuesday, July 22, 2014 1:05 PM

All replies

  • Phew....I was able to fix this after lot of diggings.... The issue was XamlReader is not recognisig staticResource. We need to define the class as a resource in Xaml, so that we can use it as static resource. Below is the code which did the trick....


    if (propertyType.ToLower() == "choice")

                        DataGridTemplateColumn outData = new DataGridTemplateColumn();
                        StringBuilder xamlData = new StringBuilder();
                        xamlData.Append("<DataTemplate ");

                        xamlData.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");
                        xamlData.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
                        xamlData.Append("xmlns:d='http://schemas.microsoft.com/expression/blend/2008' ");
                         xamlData.Append("xmlns:sys='clr-namespace:System;assembly=mscorlib' ");
                        //xamlData.Append("xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' ");
                        //xamlData.Append("xmlns:sdk='http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk'> ");
                        //xamlData.Append("<DataTemplate.Resources><local:test x:Key='test' /></DataTemplate.Resources>");
                        xamlData.Append("<local:RowIndexConverter x:Key='_rowIndexConverter' />");

                        xamlData.Append("<ComboBox SelectedItem='{Binding Path=Data,Converter={StaticResource _rowIndexConverter},ConverterParameter="+property+",Mode=TwoWay}'> ");
                        foreach (var field in test)
                            xamlData.Append("<sys:String>" + field + "</sys:String>");
                        xamlData.Append("</ComboBox> ");
                        outData.CellTemplate = (DataTemplate)System.Windows.Markup.XamlReader.Load(xamlData.ToString());
                        return outData;


    • Edited by Vino Wednesday, July 23, 2014 1:05 PM
    Wednesday, July 23, 2014 1:03 PM