none
Create Common DataGridTemplateColumn

    Question

  • hi,

    i need to create a common DataGridTemplateColumn. so that i can use it across my application with different objects and properties.

    here is the sample code i used it in my project

       <DataGridTemplateColumn Width="100*">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Path=Age}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>   
                            <DataGridTemplateColumn.CellEditingTemplate>
                                <DataTemplate>
                                    <TextBox Text="{Binding Path=Age}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellEditingTemplate>
                        </DataGridTemplateColumn>

    i need generic version of the code so that i can place the datatemplate in app.xaml and used it in my code

    regards

    Jagan


    Friday, April 27, 2012 9:11 AM

Answers

  • The code cannot work, but why you want to use it? Using the AttachedProperty, if it can work, you still need to write the Binding expression, and still need to design the different resources for the CellTemplate and CellEditingTemplate, and still need to specify the resource key to the columns. So I think it is same to use the DataGridTemplateColumn directly.

    Why the code cannot work? The DataGrid is one kind ItemsControl that uses the DataGridRow as its ItemContainer, and the DataGridRow uses the DataGridCell as its ItemContainer. So in the Visual Tree of the DataGrid, it does not have the column element, you cannot find the column element in the visual tree by Binding.RelativeSource. The column is just used in the Logical Tree, and help us to define the DataGrid. Then the DataGrid will expand the column to one Header and several cells in each DataGridRow.

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 07, 2012 8:49 AM

All replies

  • Seems we cannot do it. I think the DataGridTemplateColumn is a generic version already, you just could put any element in it and set the bindings. 

    If you want to create a more generic version, just put it in the resource dictionary, then reference it in your DataGrid.Columns, but I do not think it is make sense. The binding expression can not be set dynamically based on your bound objects in the DataGrid.

    Sincerely,



    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, May 01, 2012 10:11 AM
  • hi 

    is there possibility of using AttachedProperty or Freezable Class for Solving this Problem. I came across a code Sample that addresses this issue. but the code is also not working for me.

    they are using the attached property to communicate between DataGridTemplateColumn and DataTemplate.

    i am attaching the code . 

    Attached Property Code is 

     public static class DataBindingHelper
        {
    
            public static readonly DependencyProperty AttachedBindingProperty = DependencyProperty.RegisterAttached("AttachedBinding", typeof(Binding), typeof(DataBindingHelper), new FrameworkPropertyMetadata(null));
    
            public static Binding GetAttachedBinding(DependencyObject d)
            {
                return (Binding)d.GetValue(AttachedBindingProperty);
            }
    
            public static void SetAttachedBinding(DependencyObject d, Binding value)
            {
                d.SetValue(AttachedBindingProperty, value);
            }
    
        }

    Xaml File is

      <DataGridTemplateColumn local:DataBindingHelper.AttachedBinding="{Binding Path=Age}"  
                                                CellEditingTemplate="{StaticResource CustomCellEditingTemplate}" 
                                                CellTemplate="{StaticResource CustomCellTemplate}" Header="Age" Width="150*" />    

    App.xaml File is

     <DataTemplate x:Key="NameTemplate">
                <TextBlock Text="{Binding}"/>
            </DataTemplate>
            <DataTemplate x:Key="EditingTemplate">
                <TextBox Text="{Binding}"/>
            </DataTemplate>
    
            <DataTemplate x:Key="CustomCellTemplate">
                <ContentPresenter ContentTemplate="{StaticResource NameTemplate}" 
                                  Content="{Binding Path=(local:DataBindingHelper.AttachedBinding), RelativeSource={RelativeSource AncestorType={x:Type DataGridTemplateColumn}}}" />
            </DataTemplate>
            <DataTemplate x:Key="CustomCellEditingTemplate">
                <ContentPresenter ContentTemplate="{StaticResource EditingTemplate}"
                                  Content="{Binding Path=(local:DataBindingHelper.AttachedBinding), RelativeSource={RelativeSource AncestorType={x:Type DataGridTemplateColumn}}}" />
            </DataTemplate>

    is this code work? 

    regards

    Jagan

    Wednesday, May 02, 2012 6:05 AM
  • The code cannot work, but why you want to use it? Using the AttachedProperty, if it can work, you still need to write the Binding expression, and still need to design the different resources for the CellTemplate and CellEditingTemplate, and still need to specify the resource key to the columns. So I think it is same to use the DataGridTemplateColumn directly.

    Why the code cannot work? The DataGrid is one kind ItemsControl that uses the DataGridRow as its ItemContainer, and the DataGridRow uses the DataGridCell as its ItemContainer. So in the Visual Tree of the DataGrid, it does not have the column element, you cannot find the column element in the visual tree by Binding.RelativeSource. The column is just used in the Logical Tree, and help us to define the DataGrid. Then the DataGrid will expand the column to one Header and several cells in each DataGridRow.

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 07, 2012 8:49 AM
  • hi

    thanks for ur reply

    Monday, May 07, 2012 10:46 AM
  • I made a DatagridBoundTemplateColumn.


    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
     
    namespace OwensWpfFunStuff
    {
    public class DataGridBoundTemplateColumn : DataGridBoundColumn
    {
    public DataTemplate CellTemplate { get; set; }
    public DataTemplate CellEditingTemplate { get; set; }
     
    protected override System.Windows.FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
    {
    return Generate(dataItem, CellEditingTemplate);
    }
     
    private FrameworkElement Generate(object dataItem, DataTemplate template)
    {
    var contentControl = new ContentControl { ContentTemplate = template, Content = dataItem };
    BindingOperations.SetBinding(contentControl, ContentControl.ContentProperty, Binding);
    return contentControl;
    }
     
    protected override System.Windows.FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
    {
    return Generate(dataItem, CellTemplate);
    }
    }
    }

    • Edited by Poptart911 Tuesday, March 04, 2014 8:19 PM microsoft removed my script
    Tuesday, March 04, 2014 8:18 PM