What is the code behind for datagridtemplatecolumn, and how to use it?

15,605

Solution 1

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!!

Solution 2

Anurag's answer will work very well for you if you want to add the buttons before the grid is instantiated, specifically before you add the column to the grid.

If you want to add the button to the grid cell after the grid is already built, you can do it by making changes to the DataGridCell object. First you have to find it:

  1. Find the DataGridCell by using DataGridColumn.GetCellContent
  2. Use VisualTreeHelper to scan up the visual tree to the DataGridCell

Once this is done, there are several ways to add a button to the DataGridCell, depending on what you're trying to achieve:

  • Set DataGridCell.Template to a ControlTemplate containing the buttons and other styling you desire, -OR-
  • Set DataGridCell.ContentTemplate to a DataTemplate containing the buttons and other items you desire, -OR-
  • Have your column's DataTemplate include a placeholder panel to hold new buttons, search down the visual tree for this panel by Name, and add your button to it.

An alternative approach that doesn't require finding the cell is to:

  1. Include an ObservableCollection<T> property in your view model that supplies the information to create the buttons
  2. In your DataTemplate include an ItemsControl that reference this property and has a DataTemplate that can create the correct button out of type T
  3. When you want to add a button, just add an item to the ObservableCollection property
Share:
15,605
Tyler Woyshner
Author by

Tyler Woyshner

I started out as a newbie in the world of computer codes....and I survived for a while, and then i did my MBA! And see who's back here again!!! Somethings money can't buy...truly!

Updated on June 05, 2022

Comments

  • Tyler Woyshner
    Tyler Woyshner almost 2 years

    I have a DataGrid in WPF. And I am trying to add Buttons to certain cells of the grid, after it is bound to a particular ItemsSource. I have tried to do this in the xaml like this:

    <dg:DataGridTemplateColumn x:Name="R1" CanUserReorder="False" IsReadOnly="False">             
        <dg:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <awc:ImageButton Content="Edit" Name="btnEdit" Visibility="Collapsed"/>
            </DataTemplate>
        </dg:DataGridTemplateColumn.CellTemplate>
    </dg:DataGridTemplateColumn>
    

    However, I want to know as to how I can do this in the code behind. I need this so that I can place Buttons whenever a particular click even takes place. Any help will be highly appreciated.

  • Manish Sinha
    Manish Sinha almost 14 years
    Sorry for bumping an old thread, but in case of Button at FrameworkElementFactory(typeof(Button)) , how do I add the Button Text? I have been unable to do so, or am I missing something?
  • viky
    viky almost 14 years
    @Manish Sinha, use factory1.SetValue(Button.ContentProperty, b1); You can set any property of Button in your case in the same way I am setting the IsChecked property of CheckBox in above example.
  • Manish Sinha
    Manish Sinha almost 14 years
    Sweet! So simply!! Thanks a lot.