Click here to Skip to main content
14,732,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to add columns to silverlight columns at runtime and also perform the bindings for the columns.

This is how i do it statically in xaml
<sdk:DataGridTextColumn  CanUserReorder="True" CanUserResize="True" CanUserSort="True"  Header="CriteriaName" Width="2*" Binding="{Binding Path=[CriteriaName]}" IsReadOnly="True"  />

Now i want do the same in code behind,

here is what i have done
foreach(string Col in lColumnNames)
               DataGridTextColumn DGCol=new DataGridTextColumn();
               DGCol.Header= Col;

               Binding lObjBinding = new Binding(Col);
               lObjBinding.Mode = BindingMode.OneWay;

               DGCol.Binding = lObjBinding;


This is not working.All i can see are blank rows in the DataGrid,as if the binding hasn't happened. Pls check and tell me if all the things that i have done using xaml is done using C# as well or is there some property that is left to be set in the Binding Object that i have created.
Updated 12-Dec-12 18:26pm

1 solution

I have never tried this, but I see that the Binding (lObjBinding) does not have a Source or RelativeSource.

I think the Binding use the parent/holder's source when you add it in the XAML. In the XAML, it knows that the Source is each item in the DataGrid's ItemsSource.
When you create the binding in by code, you may have to set Source for the Binding manually.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900