Applying DataTemplate to a Grid

datatemplate, wpf, xaml

Solution

You cannot apply `DataTemplates` to panels (e.g. Grid).

If you just want that template placed somwhere then you can use a `ContentControl` and set it as the `ContentTemplate` via `StaticResource`.

(`ContentControl.Content` needs to be set to something, otherwise the `ContentTemplate` is not applied, if there is no real "content" setting the `Template` instead should work as well.)

Problem

How is a `DataTemplate` applied to a `Grid`? I have a `DataTemplate` named DataGrid_Template in my `Resources.xaml` file that I would like to apply to a `Grid` in View.xaml. Resources.xaml ``` <ResourceDictionary ... > <DataTemplate x:Key="DataGrid_Template"> <Grid> <Grid.RowDefinitions ... /> <DockPanel ... /> <DataGrid ... /> </Grid> </DataTemplate> </ResourceDictionary> ``` View.xaml ``` <UserControl ... > <Grid /> <!-- want to apply DataGrid_Template to this --> </UserControl> ``` I tried using the `Grid` property `TemplatedParent`, but that seems to be a read-only property.

Original source

Related problems