开发者

How to define implicit data templates in Metro XAML?

开发者 https://www.devze.com 2023-04-07 02:04 出处:网络
I am trying to create a DataTemplate for mapping a simple data type with a corresponding view as follows:

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>

</Use开发者_运维百科rControl>


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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号