1

I am building a database-access program that dynamically creates datagrid columns, as well as dynamically requests data based on account preferences. I have several datagrids that work in identical manners and for the most part they work perfectly. All of my text columns bind to the datatables that I use to store all of the data, however I cannot figure out how to get my template columns to bind to a datatable column. I am using the template columns as datepicker columns, if that helps at all.

The visuals load fine: i.e. The columns load perfectly in the sense that I can see and interact with them without an issue. The main problem is binding them to the datatables themselves. Any help is appreciated. Please remember that the datagrids always exist, it is only the columns that are completely dynamic.

Here is the basic version of what I have done (In wpf)

<ResourceDictionary>
    <DataTemplate x:Key="datePickerTemplate">
        <DatePicker Text="{Binding}"/>                                      
    </DataTemplate>
</ResourceDictionary>

<DataGrid x:Name="datagrid_1" ItemsSource = "{Binding}" AutoGenerateColumns = "False">

(In c#)

//creates a text column (works just fine)
DataGridTextColumn textcolumn = new DataGridTextColumn();
textcolumn.Header = "text column";
textcolumn.Binding = new Binding("bind test column"); //text columns bind fine          
datagrid_1.Columns.Add(setexpmeetdatecolumn); 

//create template column
DataGridTemplateColumn templatecolumn = new DataGridTemplateColumn();
templatecolumn.Header = "date template column";
templatecolumn.CellTemplate = (DataTemplate)FindResource("datePickerTemplate");
(problem -> ) templatecolumn.Binding = new Binding("bind test column"); //this is what I need to accomplish, but am not finding any legible answers that are coherent to understand.     

datagrid_1.Columns.Add(templatecolumn); 
caradon
  • 21
  • 3
  • Is the data that is coming out of your database dates? I am a bit confused as to why you are using datepicker for all columns as I would assume at least one column would be non-date. – Parth Shah Jun 13 '15 at 03:30
  • You are right, Parth. The majority of the columns are simply text columns that contain various pieces of information. There are only 4 columns that actually contain dates, and so there is a mix of dates and text data. – caradon Jun 13 '15 at 17:06
  • Can you try doing the binding in the manner specified in this post: http://stackoverflow.com/a/1755556/2310818? – Parth Shah Jun 15 '15 at 01:22
  • @ParthShaw That post actually shows exactly what I was originally doing, it does not really help much. I greatly appreciate that you went searching though. – caradon Jun 15 '15 at 23:08

1 Answers1

0

Okay, I figured it out. It turns out that the binding has to be done in the wpf code where the template for the template is created. No binding actually has to occur in the code-behind for the individual column. The table itself does still have to be pointed to its items source, but not the column.

The binding path has to be declared in the template in wpf like so:

<ResourceDictionary>
    <DataTemplate x:Key="datePickerTemplate">
        <DatePicker Text="{Binding Path = bind test column}"/>                                      
    </DataTemplate>
</ResourceDictionary>

and then the code-behind looks like this:

DataGridTemplateColumn templatecolumn = new DataGridTemplateColumn();
templatecolumn.Header = "date template column";
templatecolumn.CellTemplate = (DataTemplate)FindResource("datePickerTemplate");
datagrid_1.Columns.Add(templatecolumn);

I ended up figuring it out by looking at code examples that created data template columns that seemed to successfully bind to data sources. I kept seeing the "Binding Path", but no search that I went through actually described what that part of the code accomplished.

caradon
  • 21
  • 3