tags:

views:

1102

answers:

3

Lets say we have the following code in XAML (the datagrid is bound to an ObservableCollection and the column to a property of the ObservableCollection:

<WpfToolkit:DataGrid
        ItemsSource="{Binding Path=Collection}"
        HorizontalScrollBarVisibility="Hidden" SelectionMode="Extended"
        CanUserAddRows="False" CanUserDeleteRows="False"
        CanUserResizeRows="False" CanUserSortColumns="False"
     AutoGenerateColumns="False"
        RowHeaderWidth="17" RowHeight="25">
     <WpfToolkit:DataGrid.Columns>

      <WpfToolkit:DataGridTextColumn
       Header="Names" Width="2*"
       Binding="{Binding Path=Name}"/>

     </WpfToolkit:DataGrid.Columns>
</WpfToolkit:DataGrid>

How can you create a new column programmatically in C# with the binding set to a certain PropertyPath (in my case a property of an ObservableCollection)?

This is what I have right now:

Binding items = new Binding();
PropertyPath path = new PropertyPath("Name");
items.Path = path;



MyDataGrid.Columns.Add(new DataGridTextColumn()
{
   Header = "Names",
   Width =  275,
   Binding = items
});

I am pretty sure that the problem is in the PropertyPath but I do not know what I must write in it...

Thank you for any help!

+1  A: 

I do this in my program.

I use another datagrid and MVVM, but the idea should be the same.

Create a collection that holds all the columns you need, and just bind this collection to the grid in xaml.

So don't define columns in xaml in this case, only in code.

something like this:

<WpfToolkit:DataGrid
        ItemsSource="{Binding Path=Collection}"
        HorizontalScrollBarVisibility="Hidden" SelectionMode="Extended"
        CanUserAddRows="False" CanUserDeleteRows="False"
        CanUserResizeRows="False" CanUserSortColumns="False"
        AutoGenerateColumns="False"
        RowHeaderWidth="17" RowHeight="25"
        Columns="{Binding Path=ColumnCollection}"
/>
Natrium
I am defining my columns in code... I need to know how to bind a column to collection property. In my case, the collection is an ObservableCollection of a certain class that contains properties.
Partial
WPFToolkik DataGrid doesn't support binding to columns in the current version. Maybe soon?
Jake Pearson
I use infragistics-datagrid.
Natrium
@Jakers: In fact, column binding is supported... You can create an observablecollection of a class that you create that contains the properties and bind the properties to the columns.
Partial
@Partial You're right. I was speaking to the Columns property of datagrid, it has no setter so you can't bind your own collection of columns to it.
Jake Pearson
+1  A: 

I have almost the exact same code as you, I just create the binding in a slightly different way:

void Add(ColumnViewModel columnViewModel)
{
 var column = new DataGridTextColumn
 {
  Header = columnViewModel.Name,
  Binding = new Binding("[" + columnViewModel.Name + "]")
 };
 dataGrid.Columns.Add(column);
}
Jake Pearson
A: 

The reason my code was not working is that I was not writing the good property for the path. Thank you anyway for the suggestions!

Partial
Perhaps for this answer to be useful you can write the correct property that worked?
Maslow