如何在 Metro XAML 中定义隐式数据模板?
我正在尝试创建一个 DataTemplate,用于将简单数据类型映射到相应的视图,如下所示:
<DataTemplate DataType="{x:Type src:Person}">
<TextBox Text="{Binding Name}"/>
</DataTemplate>
我收到编译器错误,指示无法识别或访问 DataType 属性。我在这里错过了什么吗?是否有新的语法可以执行此操作,或者该功能是否缺失?隐式模板是否有替代解决方案?
作为参考,这里是使用 ax:Key 属性限定的 DataTemplate 的完整代码(有效):
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于 WinRT,将 CLR 命名空间映射到 XAML 的语法有所不同。您应该将映射从:
更改
为 有关从 Silverlight 迁移到 WinRT 的更多信息,请参阅 Morten Nielsen 的一系列博客文章,或我写的关于创建跨平台 Silverlight / WinRT 应用程序。
但是...如果您查看 API 文档对于DataTemplate,你会发现没有DataType属性。 WinRT 中存在隐式样式,但没有隐式数据模板。
With WinRT, the syntax for mapping your CLR namespaces to XAML are different. You should change you mapping from:
to
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.
Silverlight 没有
DataTemplate.DataType
,我怀疑 Windows XAML 框架继承了该限制。您可能必须改用 DataTemplateSelector。有趣的是,它确实有
DataTemplateKey
类,但从 XAML 实例化它不起作用。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.你定义了命名空间吗?
xmlns:src="clr-namespace:WpfApplicationNamespace"
Have you define namespace?
xmlns:src="clr-namespace:WpfApplicationNamespace"