Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
440
How to bind the template columns from code behind means runtime.
posted

I have bind the data to xamgrid columns and data dynamically. i am using templatecolumn and loading the datatemplate xaml at runtime.

sb.Append("<DataTemplate >");
//sb.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");
//sb.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> ");

sb.Append("<Grid>");
sb.Append("<TextBlock ");
sb.Append("Text = '{Binding ");
sb.Append(item.DataField + "}' ></TextBlock>");
sb.Append("</Grid>");
sb.Append("</DataTemplate>");

Creation of xamgrid columns at runtime :

var col = new TemplateColumn();

col.Key = item.DataField;
col.HeaderText = item.HeaderText;

col.ItemTemplate = (DataTemplate)XamlReader.Load(GetXamlForColumn(item));

grid.Columns.Add(col);

But i am not able to render the grid. Please help me if i am doing any thing wrong.

  • 138253
    Verified Answer
    Offline posted

    Hello,

     

    Thank you for your post. I have been looking into and I created a sample project for you using your code, but with little modifications. The namespaces should be added in the DataTemplate opening tag, not after it. Please let me know if this helps you or you need further assistance on this matter.

     

    Looking forward for your reply.

    XamGridAddColumnsRunTime.zip