views:

790

answers:

2

I was able to recreate this XAML DataGridTextColumn:

<tk:DataGridTextColumn
    Binding="{Binding FirstName}"
    Header="First Name"/>

in code like this:

DataGridTextColumn dgtc = new DataGridTextColumn();
dgtc.Header = propertyLabel;
dgtc.Binding = new Binding(propertyName);
theDataGrid.Columns.Add(dgtc);

But how do I recreate the following DataGridTemplateColumn in code?

<tk:DataGridTemplateColumn Width="100">
    <tk:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Style="{DynamicResource ManageLinkStyle}"
                    Tag="{Binding Id}" Text="Delete" MouseDown="System_Delete_Click"/>
                <TextBlock Text=" "/>
                <TextBlock Style="{DynamicResource ManageLinkStyle}"
                           Tag="{Binding Id}" Text="Edit" MouseDown="System_Edit_Click"/>
            </StackPanel>
        </DataTemplate>
    </tk:DataGridTemplateColumn.CellTemplate>
</tk:DataGridTemplateColumn>

i.e. I'm getting stuck on defining the CellTemplate:

DataGridTemplateColumn dgTemplateColumn = new DataGridTemplateColumn();
dgTemplateColumn.CellTemplate = new CellTemplate ...???

Answer:

Thank you Aran, just referring to the template key in XAML works well for what I needed, here is how I changed the above to work for me:

XAML:

<Window.Resources>
    <DataTemplate x:Key="manageAreaCellTemplate">
        <StackPanel Orientation="Horizontal">
            <TextBlock Style="{DynamicResource ManageLinkStyle}"
        Tag="{Binding Id}" Text="Delete" MouseDown="System_Delete_Click"/>
            <TextBlock Text=" "/>
            <TextBlock Style="{DynamicResource ManageLinkStyle}"
               Tag="{Binding Id}" Text="Edit" MouseDown="System_Edit_Click"/>
        </StackPanel>
    </DataTemplate>
</Window.Resources>

code-behind:

DataGridTemplateColumn dgTemplateColumn = new DataGridTemplateColumn();
dgTemplateColumn.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
dgTemplateColumn.Header = "Manage Options";
dgTemplateColumn.CellTemplate = this.FindResource("manageAreaCellTemplate") as DataTemplate;
theDataGrid.Columns.Add(dgTemplateColumn);
A: 

use this:

DataGridTemplateColumn col1 = new DataGridTemplateColumn();
col1.Header = "MyHeader";
FrameworkElementFactory factory1 = new FrameworkElementFactory(typeof(CheckBox));
Binding b1 = new Binding("IsSelected");
b1.Mode = BindingMode.TwoWay;
factory1.SetValue(CheckBox.IsCheckedProperty, b1);
factory1.AddHandler(CheckBox.CheckedEvent, new RoutedEventHandler(chkSelect_Checked));
DataTemplate cellTemplate1 = new DataTemplate();
cellTemplate1.VisualTree = factory1;
col1.CellTemplate = cellTemplate1;
dgTransportReqsts.DataGrid.Columns.Add(col1);

I used this to add CheckBox in my DataGridTemplateColumn at runtime. Hope this helps!!

viky
+1  A: 

would it be acceptable to you to define the cell template in xaml still?

then you could define it

<DataTemplate x:Key="myCellTemplateKey">
    <StackPanel Orientation="Horizontal">
        <TextBlock Style="{DynamicResource ManageLinkStyle}"
            Tag="{Binding Id}" Text="Delete" MouseDown="System_Delete_Click"/>
        <TextBlock Text=" "/>
        <TextBlock Style="{DynamicResource ManageLinkStyle}"
                   Tag="{Binding Id}" Text="Edit" MouseDown="System_Edit_Click"/>
    </StackPanel>
</DataTemplate>

and then reference it from your code behind when you create the xaml.

dgTemplateColumn.CellTemplate = this.FindResource("myCellTemplateKey") as DataTemplate;

using FrameworkElementFactory to create data templates is fiddly at best, and you never know when they are going to change something in the framework so your behaviour is not defined as you expected(cant remember where i read that, sorry). the only time i use it is when i create a data template that has only a content control. then i use the standard data templating to display the object in that content control. but using it is usually a hack.

Aran Mulholland
good idea, worked excellently, and produces much cleaner code, thanks!
Edward Tanguay
glad it worked, mark it as answered if it is.
Aran Mulholland