If your DataTemplate
s are exactly the same then just put one into a Resources
section and then simply reference it:
<DataGrid.Resources>
<DataTemplate x:Key="TheDataTemplate">
<TextBlock
Background="{Binding _5_5, Converter={StaticResource ResourceKey=hourSlotColorConverter}, UpdateSourceTrigger=PropertyChanged}"
Margin="-1">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonDown">
<i:InvokeCommandAction
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
Path=DataContext.StartCellCommand}">
<i:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource dgInfoConverter}">
<Binding Path="Column"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
<Binding Path="DataContext"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
</MultiBinding>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
<i:EventTrigger EventName="PreviewMouseLeftButtonUp">
<i:InvokeCommandAction
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
Path=DataContext.LastCellCommand}">
<i:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource dgInfoConverter}">
<Binding Path="Column"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
<Binding Path="DataContext"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
</MultiBinding>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
<i:EventTrigger EventName="MouseMove">
<i:InvokeCommandAction
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
Path=DataContext.MouseDragCom}">
<i:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource dgInfoConverter}">
<Binding Path="Column"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
<Binding Path="DataContext"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}" />
</MultiBinding>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
</DataTemplate>
</DataGrid.Resources>
...
<DataGridTemplateColumn CellTemplate="{StaticResource TheDataTemplate}"
Header="" MaxWidth="10" />
...
UPDATE >>>
Ok, so if you have different Binding
s inside the DataTemplate
, then you'll have to use a UserControl
instead. You will then be able to declare DependencyProperty
s for the variable properties and bind to them from the DataGridColumn
:
<UserControl ... >
<TextBlock
Background="{Binding Background, RelativeSource={RelativeSource AncestorType={x:Type yourXmlNamespacePrefix:YourUserControl}}}"
Margin="-1">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonDown">
<i:InvokeCommandAction
Command="{Binding StartCellCommand, RelativeSource={RelativeSource AncestorType={x:Type yourXmlNamespacePrefix:YourUserControl}}}">
<i:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource dgInfoConverter}">
<Binding Path="Column"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type yourXmlNamespacePrefix:YourUserControl}}" />
<Binding Path="SomeProperty"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type yourXmlNamespacePrefix:YourUserControl}}" />
</MultiBinding>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
...
</i:Interaction.Triggers>
</TextBlock>
</UserControl>
If you define a DependencyProperty
for each different Binding
, then you'll be able to set them like this:
...
<DataGridTemplateColumn Header="" MaxWidth="10">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<yourXmlNamespacePrefix:YourUserControl Background="{Binding _5_5,
Converter={StaticResource ResourceKey=hourSlotColorConverter},
UpdateSourceTrigger=PropertyChanged}" StartCellCommand="{Binding RelativeSource=
{RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.StartCellCommand}"
Column="{Binding Column, RelativeSource={RelativeSource AncestorType={x:Type
DataGridCell}}}" SomeProperty="{Binding DataContext, RelativeSource={RelativeSource
AncestorType={x:Type DataGridCell}}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn >
...
It's still a lot of code, but it's much less than before. I didn't finish this example for all of your properties, but I'm guessing that you'll get the idea.