Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define implicit data templates in Metro XAML?

I am trying to create a DataTemplate for mapping a simple data type with a corresponding view as follows:

<DataTemplate DataType="{x:Type src:Person}">
    <TextBox Text="{Binding Name}"/>
</DataTemplate>

I get a compiler error indicating that the DataType property is not recognized or accessible. Am I missing something here? Is there new syntax for doing this or is the feature missing? Are there alternative solutions for implicit templates?

For reference, here is the full code with the DataTemplate qualified using a x:Key attribute (which works):

<UserControl x:Class="Metro_App.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:src="clr-namespace:Metro_App"
    mc:Ignorable="d"
    d:DesignHeight="768" d:DesignWidth="1366">

    <UserControl.Resources>        
        <DataTemplate x:Key="PersonTemplate">
            <TextBlock Text="{Binding Name}" Foreground="White" FontSize="72"/>
        </DataTemplate>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="#FF0C0C0C">
        <ContentControl Content="{Binding MyPerson}" ContentTemplate="{StaticResource PersonTemplate}"/>
    </Grid>

</UserControl>
like image 844
robzhu Avatar asked Sep 23 '11 04:09

robzhu


2 Answers

With WinRT, the syntax for mapping your CLR namespaces to XAML are different. You should change you mapping from:

xmlns:src="clr-namespace:Metro_App"

to

xmlns:src="using:Metro_App"

For further information on moving from Silverlight to WinRT, see the series of blog posts by Morten Nielsen, or the article I wrote about creating a cross platform Silverlight / WinRT application.

However ... if you look at the API documentation for DataTemplate you will find that there is not DataType property. Within WinRT there is implicit styling, but not implicit data templating.

like image 97
ColinE Avatar answered Nov 07 '22 21:11

ColinE


Silverlight doesn't have DataTemplate.DataType, and I suspect that Windows XAML framework inherited that limitation. You might have to use DataTemplateSelector instead.

Interestingly enough, it does have DataTemplateKey class, but instantiating it from XAML does not work.

like image 44
Pavel Minaev Avatar answered Nov 07 '22 20:11

Pavel Minaev