Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 894
mauryat Avatar asked Sep 02 '25 03:09

mauryat


1 Answers

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

like image 170
H.B. Avatar answered Sep 05 '25 01:09

H.B.