将DataTemplate应用于网格

mau*_*yat 3 wpf xaml datatemplate

如何DataTemplate应用于Grid

我有一个DataTemplate名为DataGrid_Template在我的Resources.xaml文件,我想申请到GridView.xaml.


Resources.xaml

<ResourceDictionary ... >
    <DataTemplate x:Key="DataGrid_Template">
        <Grid>
            <Grid.RowDefinitions ... />
            <DockPanel ... />
            <DataGrid ... />
        </Grid>
    </DataTemplate>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)


View.xaml

<UserControl ... >
    <Grid /> <!-- want to apply DataGrid_Template to this -->
</UserControl>
Run Code Online (Sandbox Code Playgroud)


我尝试使用该Grid 属性 TemplatedParent,但这似乎是一个只读属性.

H.B*_*.B. 10

您无法应用于DataTemplates面板(例如网格).

如果您只想将该模板放置在somwhere,那么您可以使用a ContentControl并将其设置为ContentTemplatevia StaticResource.

(ContentControl.Content需要设置为某些内容,否则ContentTemplate不应用,如果没有真正的"内容"设置,Template则应该也可以使用.)

  • 为了添加这个答案,我还必须在 ContentControl 上设置 Content={Binding} 才能使其正常工作(从这里得到这个想法http://stackoverflow.com/a/15389304/2868438) (2认同)