2011-09-23 28 views
5

我想创建一个DataTemplate用于如下用相应的视图映射为简单数据类型:如何在Metro XAML中定义隐式数据模板?

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

我得到指示的数据类型属性无法识别或访问的编译器错误。我在这里错过了什么吗?有没有新的语法做这个或缺少的功能?是否有隐式模板的替代解决方案?

供参考,在这里与DataTemplate中的全部代码使用合格的X:Key特性(工作):

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

回答

9

使用WinRT,将CLR命名空间映射到XAML的语法是不同的。你应该改变你的映射从:

xmlns:src="clr-namespace:Metro_App" 

xmlns:src="using:Metro_App" 

如需进一步信息从Silverlight的移动WinRT中,看到series of blog posts by Morten Nielsen或文章中,我写了一篇关于creating a cross platform Silverlight/WinRT application。如果你看看API documentation for DataTemplate you will find that there is not DataType property。在WinRT中有隐式样式,但不包含隐式数据模板。

+0

然而,有'DataTemplateKey',这是很有趣的。 –

+0

是的。也许暗示隐含的模板正在他们的路上? – ColinE

-3

你有没有定义命名空间? 的xmlns:SRC = “CLR的命名空间: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> 
+0

是的,我有。我已经粘贴了完整的MainPage.xaml编辑了我的原始文章。 – robzhu

+1

@Radik WinRT对namepsace映射使用了不同的语法。 – ColinE

+0

现在很难过现在我们应该知道他们两个 – Radik

2

Silverlight不具有DataTemplate.DataType,我怀疑的Windows XAML框架继承了限制。您可能必须改用DataTemplateSelector

有趣的是,它确实有DataTemplateKey类,但从XAML实例化它不起作用。

相关问题