views:

7256

answers:

2

I would like to be able to programmatically create DataGridTemplateColumns based on my data source. For example, if my source has a date in a particular column I would like to be able to utilize a Datepicker control. I know this is easily accomplished with xaml and a DataGridTemplateColumn at design-time, however, how would I accomplish this at run-time?

Is my best option xamlreader.load or a more traditional route like:

Dim TempCol As Microsoft.Windows.Controls.DataGridTemplateColumn

I have not had any success with the latter.

Thanks.

-Paul

Edit: This is the code I attempted to use:

        Dim TempCol As New Microsoft.Windows.Controls.DataGridTemplateColumn

    TempCol.CellEditingTemplate = DataTemplate.Equals(DatePicker)

I receive DatePicker is a type and cannot be used as an expression.

I am basiing this on the WPF Toolkit demo. http://windowsclient.net/wpf/wpf35/wpf-35sp1-toolkit-datagrid-feature-walkthrough.aspx

<dg:DataGridTemplateColumn Header="Date" MinWidth="100">
    <dg:DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <dg:DatePicker SelectedDate="{Binding Date}" SelectedDateFormat="Short" />
        </DataTemplate>
    </dg:DataGridTemplateColumn.CellEditingTemplate>
    <dg:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Date, StringFormat=d}" />
        </DataTemplate>
    </dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>

Thanks!

+2  A: 

The reason that your code does not work is because you are setting the value of the CellEditingTemplate column to a bool (the result of calling DataTemplate.Equals(), rather than creating an instance of the template in code.

You can create a template in code using something like this (equivalent to the XAML code snippet you provided):

DataGridTemplateColumn col = new DataGridTemplateColumn();
col.Header = "Date";

// Create a factory. This will create the controls in each cell of this
// column as needed.
FrameworkElementFactory factory =
    new FrameworkElementFactory(typeof(DatePicker));

// Bind the value of this cell to the value of the Date property of the
// DataContext of this row. The StringFormat "d" will be used to display
// the value.
Binding b = new Binding("Date");
b.StringFormat = "d";
factory.SetValue(DatePicker.SelectedDateProperty, b);

// Create the template itself, and add the factory to it.
DataTemplate cellEditingTemplate = new DataTemplate();
cellEditingTemplate.VisualTree = factory;

col.CellEditingTemplate = cellEditingTemplate;

I'm not sure if this approach would work better than loading the XAML yourself. Maybe try both approaches and see which one works best for you, and works faster?

Andy
Andy,Thanks for your answer. I was under the impression though that FrameworkElementFactory would not be supported in the future.See this answer as a reference.http://stackoverflow.com/questions/617052/how-can-i-convert-this-xaml-code-into-c-code/617799#617799Thanks again,-Paul
PaulWaldman
I haven't heard about that one way or the other. Sounds like XamlReader might be the way to go, then.
Andy
A: 

beri thanks 4 ur input

Mike Angel FROM Tabasco Méxic