3

I have below DataGrid(simplified)

<DataGrid ItemsSource="{Binding Something}"> <DataGrid.Columns> <DataGridTemplateColumn Header="Test"> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <Grid> <TextBox Text="{Binding A}"/> </Grid> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Grid> <TextBlock Text="{Binding A}"/> </Grid> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTemplateColumn Header="Test"> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <Grid> <TextBox Text="{Binding B}"/> </Grid> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Grid> <TextBlock Text="{Binding B}"/> </Grid> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </Datagrid> 

Is there any way to move DataTemplate to resources and reuse it for different properties so I don't have to copy and paste DataTemplate for every property?

2 Answers 2

6

A pragmatic solution would be to define templates on resource level and wrap them in a 'ContentPresenter' in each column. You still have to define a template for each column explicitly. Still, you can manage the templates in one place and quickly see to which properties your columns are bound.

<DataGrid> <!-- Templates in a single place in resources --> <DataGrid.Resources> <DataTemplate x:Key="CellTemplate"> <TextBlock Text="{Binding}" /> </DataTemplate> <DataTemplate x:Key="EditCellTemplate"> <TextBlock Text="{Binding}" /> </DataTemplate> </DataGrid.Resources> <DataGrid.Columns> <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ContentPresenter Content="{Binding A}" ContentTemplate="{StaticResource CellTemplate}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <ContentPresenter Content="{Binding A}" ContentTemplate="{StaticResource EditCellTemplate}" /> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> </DataGridTemplateColumn> <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ContentPresenter Content="{Binding B}" ContentTemplate="{StaticResource CellTemplate}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <ContentPresenter Content="{Binding B}" ContentTemplate="{StaticResource EditCellTemplate}" /> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> 
Sign up to request clarification or add additional context in comments.

1 Comment

For me, the key from your answer that got me functioning was setting the Content="{Binding}" on the ContentPresenter. I was using DataContext="{Binding}" which didn't work, but after changing it to Content I got the binding to finally work.
3

You could do it like this:

<DataGrid ItemsSource="{Binding Something}"> <DataGrid.Resources> <DataTemplate x:key="MyTemplate"> <Grid> <TextBox Text="{Binding}"/> </Grid> </DataTemplate> </DataGrid.Resources> <DataGrid.Columns> <DataGridTemplateColumn Header="Test" CellTemplate="{StaticResource MyTemplate}" > </DataGridTemplateColumn> </DataGrid.Columns> </Datagrid> 

But then you need to rethink binding logic, as you will need to use cell datacontext in the same template for all the columns.

4 Comments

As far as I understand {Binding} binds whole object to template , so how I can bind specific property to template column?
@blackcat there is nothing stopping you from having two datatemplates inside the resources (one for when binding to A and one when B), still better than four.
@adminSoftDK still I need four datatemplates (edit and display where I will have {Binding A} and {Binding B}), I am looking generic solution for this
@adminSoftDK make all your bound classes adhere to an Interface which the template only binds to, that way one only needs one template per many different object types.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.