Johan Larsson
Johan Larsson

Reputation: 17580

How to get ContentControl to resolve DataTemplate

Why does this not resolve the datatemplate?

<Window.Resources>
    <DataTemplate DataType="system:DateTime" >
        <Grid Background="Aqua">
            <TextBlock Text="{Binding Day}"></TextBlock>
        </Grid>
    </DataTemplate>
</Window.Resources>
<Grid>
    <ContentControl Content="{x:Static system:DateTime.Now}"/>
</Grid>

Writing a TemplateSelector feels like an overkill.

Upvotes: 4

Views: 437

Answers (1)

Anatoliy Nikolaev
Anatoliy Nikolaev

Reputation: 22702

DataType design suggests the presence of a directive x:Type like that:

<DataTemplate DataType="{x:Type system:DateTime}">
    <Grid Background="Aqua">
        <TextBlock Text="{Binding Day}" Height="30" Width="100" HorizontalAlignment="Center" />
    </Grid>
</DataTemplate>

See the MSDN for more information.

Upvotes: 3

Related Questions