mauryat
mauryat

Reputation: 1646

Applying DataTemplate to a Grid

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.

Upvotes: 4

Views: 17602

Answers (1)

brunnerh
brunnerh

Reputation: 185290

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.)

Upvotes: 14

Related Questions