wpftoolkit DataGridTemplateColumn Template binding

Posted by Guillaume on Stack Overflow See other posts from Stack Overflow or by Guillaume
Published on 2012-03-22T16:44:15Z Indexed on 2012/03/22 17:29 UTC
Read the original article Hit count: 273

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

© Stack Overflow or respective owner

Related posts about wpf

Related posts about xaml