In my application I have a Window
with three DataGrid
s on it. All DataGrid
s should have the same colums.
Two of the DataGrid
s are bound to an ObservableCollection<DisplayItem>
. The third DataGrid
is bound to an ObservableCollection<GeneralItem>
.
The class GeneralItem
has a property of type DisplayItem
.
The definition of the DataGridTemplateColumn
for the Item-Name in two of three DataGrid
s looks like:
<DataGridTemplateColumn Width="Auto" Header="Item-Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Style="{StaticResource DataGridTextBlockStyle}"
Text="{Binding Module.DisplayItem.Text}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
The definition of the DataGridTemplateColumn
for the Item-Name in the third DataGrid
looks like:
<DataGridTemplateColumn Width="Auto" Header="Item-Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Style="{StaticResource DataGridTextBlockStyle}"
Text="{Binding Module.GeneralItem.DisplayItem.Text}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
I know that I can make DataGridTemplateColumn
s to resources and use it as StaticResource
s.
What I now want to know: Is there a possibility that I only have to write the DataGridTemplateColumn
once and it can handle both situations? For example by providing a DataContext to the StaticResource or so.