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

发布于 2024-12-06 11:21:37 字数 1251 浏览 2 评论 0原文

我正在尝试创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屋顶上的小猫咪 2024-12-13 11:21:37

对于 WinRT,将 CLR 命名空间映射到 XAML 的语法有所不同。您应该将映射从:

xmlns:src="clr-namespace:Metro_App"

更改

xmlns:src="using:Metro_App"

为 有关从 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:

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.

神妖 2024-12-13 11:21:37

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.

揪着可爱 2024-12-13 11:21:37

你定义了命名空间吗?
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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文