Click here to Skip to main content
12,954,403 members (90,895 online)
Rate this:
 
Please Sign up or sign in to vote.
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;
                    GrdQuickFindResult.Columns.Add(DGCol);
                    
                    
                }


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.
Thanx
Posted 12-Dec-12 3:32am
Updated 12-Dec-12 17:26pm
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hello
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.
  Permalink  

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,524
CHill60 3,490
Maciej Los 3,123
ppolymorphe 2,030
Jochen Arndt 1,975


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 15 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100