Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7682401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:33:38+00:00 2026-05-31T18:33:38+00:00

I want my datagrid columns to share a cell/celledit template. I have the solution

  • 0

I want my datagrid columns to share a cell/celledit template.

I have the solution do that (thanks to WPF DataGridTemplateColumn shared template?). Now what I would love to is improving the readability by avoiding all the node nesting.

My current view looks like that:

  <wpftk:DataGrid ItemsSource="{Binding Tests}" AutoGenerateColumns="False">

    <wpftk:DataGrid.Resources>
      <DataTemplate x:Key="CustomCellTemplate">
        <TextBlock Text="{TemplateBinding Content}"/>
      </DataTemplate>
      <DataTemplate x:Key="CustomCellEditingTemplate">
        <TextBox Text="{TemplateBinding Content}"></TextBox>
      </DataTemplate>
    </wpftk:DataGrid.Resources>

    <wpftk:DataGrid.Columns>

      <wpftk:DataGridTemplateColumn Header="Start Date">
        <wpftk:DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
            <ContentPresenter ContentTemplate="{StaticResource CustomCellTemplate}" Content="{Binding StartDate}"/>
          </DataTemplate>
        </wpftk:DataGridTemplateColumn.CellTemplate>
        <wpftk:DataGridTemplateColumn.CellEditingTemplate>
          <DataTemplate>
            <ContentPresenter ContentTemplate="{StaticResource CustomCellEditingTemplate}" Content="{Binding StartDate}"/>
          </DataTemplate>
        </wpftk:DataGridTemplateColumn.CellEditingTemplate>
      </wpftk:DataGridTemplateColumn>

      <!--and again the whole block above for each columns...-->

    </wpftk:DataGrid.Columns>
  </wpftk:DataGrid>

What I would like to achieve is to bind the value at the DataGridTemplateColumn level and propagate it to the template level. Anyone know how to do that?

What I tried to do is something like that:

  <wpftk:DataGrid ItemsSource="{Binding Tests}" AutoGenerateColumns="False">

    <wpftk:DataGrid.Resources>
      <DataTemplate x:Key="CustomCellTemplate">
        <TextBlock Text="{Binding}"/>
      </DataTemplate>
      <DataTemplate x:Key="CustomCellEditingTemplate">
        <TextBox Text="{Binding}"></TextBox>
      </DataTemplate>
    </wpftk:DataGrid.Resources>

    <wpftk:DataGrid.Columns>
      <wpftk:DataGridTemplateColumn Header="Start Date" Binding="{Binding StartDate}" CellTemplate="{StaticResource CustomCellTemplate}" CellEditingTemplate="{StaticResource CustomCellEditingTemplate}"/>
      <wpftk:DataGridTemplateColumn Header="End Date" Binding="{Binding EndDate}" CellTemplate="{StaticResource CustomCellTemplate}" CellEditingTemplate="{StaticResource CustomCellEditingTemplate}"/>
    </wpftk:DataGrid.Columns>
  </wpftk:DataGrid>

Obviously the binding porperty is not a valid property of the DataGridTemplateColumn but maybe by playing with the datacontext and some relative source could do the trick but frankly I can’t find a way to implement that.

Not sure if what I want is possible and i’m willing to accept a “no way you can do that” as an answer

NOTE: The TextBlock/TextBox in the template is just for test (the real template is much more complex) DataGridTextColumn will not do the trick
Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:33:40+00:00Added an answer on May 31, 2026 at 6:33 pm

    What you tried should have worked as you reduced the XAML by specifying the DataTemplates using the property syntax.

    Edit: Sorry. I misinterpreted your question. To add a bindable property to DataGridTemplateColumn without modifying or access to the source code you can create an AttachedProperty.

    Example:

    public class DataBindingHelper 
    {
    
        #region AttachedBinding
    
        public static readonly DependencyProperty AttachedBindingProperty = DependencyProperty.RegisterAttached("AttachedBinding", typeof(Binding), typeof(DataBindingHelper), new FrameworkPropertyMetadata(null));
    
        public static Binding GetUseAncestorDataContext(DependencyObject d)
        {
            return (bool)d.GetValue(AttachedBindingProperty);
        }
    
        public static void SetUseAncestorDataContext(DependencyObject d, Binding value)
        {
            d.SetValue(AttachedBindingProperty, value);
        }
    
        #endregion
    
    }
    

    Usage:

    <wpftk:DataGrid ItemsSource="{Binding Tests}" AutoGenerateColumns="False">
    
        <wpftk:DataGrid.Resources>
            <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=(helpers:DataBindingHelper.AttachedBinding), RelativeSource={RelativeSource AncestorType={x:Type wpftk:DataGridTemplateColumn}}}" />
            </DataTemplate>
            <DataTemplate x:Key="CustomCellEditingTemplate">
                <ContentPresenter ContentTemplate="{StaticResource EditingTemplate}"
                                  Content="{Binding Path=(helpers:DataBindingHelper.AttachedBinding), RelativeSource={RelativeSource AncestorType={x:Type wpftk:DataGridTemplateColumn}}}" />                            
            </DataTemplate>
    
        </wpftk:DataGrid.Resources>
    
        <wpftk:DataGrid.Columns>
            <wpftk:DataGridTemplateColumn Header="Start Date" helpers:DataBindingHelper.AttachedBinding="{Binding Path=StartDate}" CellTemplate="{StaticResource CustomCellTemplate}" CellEditingTemplate="{StaticResource CustomCellEditingTemplate}"/>
            <wpftk:DataGridTemplateColumn Header="End Date" helpers:DataBindingHelper.AttachedBinding="{Binding Path=EndDate}" CellTemplate="{StaticResource CustomCellTemplate}" CellEditingTemplate="{StaticResource CustomCellEditingTemplate}"/>
        </wpftk:DataGrid.Columns>
    
    </wpftk:DataGrid>
    

    To learn more about Attached Properties: read the MSDN documentation or any WPF/C# book. They are one of the most powerful things about the data binding system in WPF.


    Also if you want to know more about debugging and iterating a WPF application read my recent answer on the topic. Snoop especially would have been helpful for you understand what was going wrong with the above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF DataGrid that I want to be able to Update, Insert
I have a WPF Datagrid with a Text Column, two Template Columns, and two
i have a datagrid that contains 3 DataGridTemplateColumns that hold radioButtons. now i want
I want to show datagrid content (rows,columns) in XPS document.I have 20 columns. When
I have a datagrid that I want to add a button/s to at runtime.
I have found that datagrid columns can be dynamically created and bound in Silverlight.
I have a DataGrid, and in that grid, some of the columns are marked
I've set stuff in a DataGridTemplateColumn.CellEditing DataTemplate. I want that when the cell editing
I have a DataGrid and I want to populate a column that contains a
I have a WPF Toolkit datagrid with mulitple columns. I am trying to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.