How to define implicit data templates in Metro XAML?
Asked Answered
P

3

5

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>
Predominant answered 23/9, 2011 at 4:0 Comment(0)
B
9

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.

Bosch answered 23/9, 2011 at 8:0 Comment(2)
There is DataTemplateKey, however, which is intriguing.Priest
Yes, it is. Perhaps an indication that implicit templates are on their way?Bosch
P
2

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.

Priest answered 23/9, 2011 at 5:34 Comment(0)
A
-3

Have you define namespace? xmlns:src="clr-namespace:WpfApplicationNamespace"

<Window x:Class="WpfApplicationNamespace.MainWindow"
    xmlns:src="clr-namespace:WpfApplicationNamespace"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">

<Window.Resources>
    <DataTemplate DataType="{x:Type  src:Persone}"/>
</Window.Resources>
<Grid>
    <StackPanel Orientation="Vertical">
        <Button Content="fffff" Click="Button_Click" />
    </StackPanel>
</Grid>
</Window>
Alpert answered 23/9, 2011 at 4:41 Comment(3)
Yes I have. I have edited my original post with the full MainPage.xaml pasted.Predominant
@Alpert WinRT uses a different syntax for namepsace mappings.Bosch
it's sad now we should know both of themAlpert

© 2022 - 2024 — McMap. All rights reserved.