加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

如何在Metro XAML中定义隐式数据模板?

发布时间:2020-12-14 02:03:01 所属栏目:Windows 来源:网络整理
导读:我正在尝试创建一个DataTemplate,用于映射具有相应视图的简单数据类型,如下所示: DataTemplate DataType="{x:Type src:Person}" TextBox Text="{Binding Name}"//DataTemplate 我收到一个编译器错误,指示无法识别或访问DataType属性.我在这里错过了什么吗?
我正在尝试创建一个DataTemplate,用于映射具有相应视图的简单数据类型,如下所示:

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

我收到一个编译器错误,指示无法识别或访问DataType属性.我在这里错过了什么吗?是否有新的语法来执行此操作或缺少功能?是否存在隐式模板的替代解决方案?

作为参考,这里是使用x: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>

解决方法

使用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中有隐式样式,但不是隐式数据模板.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读