Binding a datagrid template column (as datepicker column) to a datatable column

945 views Asked by At

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); 
1

There are 1 answers

0
caradon On BEST ANSWER

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.