2011-09-23 28 views
5

tôi đang cố gắng để tạo ra một DataTemplate cho ánh xạ một kiểu dữ liệu đơn giản với một cái nhìn tương ứng như sau:Làm cách nào để xác định mẫu dữ liệu ngầm trong Metro XAML?

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

tôi nhận được một lỗi biên dịch chỉ ra rằng tài sản DataType không được công nhận hoặc tiếp cận. Am i thiếu cái gì ở đây? Có cú pháp mới để thực hiện việc này hay thiếu tính năng này? Có các giải pháp thay thế cho các mẫu ẩn không?

Để tham khảo, đây là đoạn code đầy đủ với các DataTemplate đủ điều kiện sử dụng x: attribute chính (có công trình):

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

Trả lời

9

Với WinRT, cú pháp để ánh xạ các không gian tên CLR của bạn với XAML là khác nhau. Bạn nên thay đổi bạn ánh xạ từ:

xmlns:src="clr-namespace:Metro_App" 

để

xmlns:src="using:Metro_App" 

Để biết thêm thông tin về di chuyển từ Silverlight để WinRT, xem series of blog posts by Morten Nielsen, hoặc bài báo tôi đã viết về creating a cross platform Silverlight/WinRT application.

Tuy nhiên ... nếu bạn nhìn vào số API documentation for DataTemplate you will find that there is not DataType property. Trong WinRT có kiểu dáng tiềm ẩn, nhưng không tạo ra dữ liệu tiềm ẩn.

+0

Có 'DataTemplateKey', tuy nhiên, điều này rất hấp dẫn. –

+0

Vâng, đúng thế. Có lẽ một dấu hiệu cho thấy các mẫu tiềm ẩn đang trên đường? – ColinE

-3

Bạn đã xác định không gian tên? 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

Có, tôi có. Tôi đã chỉnh sửa bài đăng gốc của mình bằng dán đầy đủ MainPage.xaml. – robzhu

+1

@Radik WinRT sử dụng cú pháp khác nhau cho ánh xạ namepsace. – ColinE

+0

Thật buồn khi chúng ta nên biết cả hai số này – Radik

2

Silverlight không có DataTemplate.DataType, và tôi nghi ngờ rằng khuôn khổ của Windows XAML thừa hưởng giới hạn đó. Bạn có thể phải sử dụng DataTemplateSelector để thay thế.

Điều thú vị là nó có DataTemplateKey lớp học, nhưng khởi tạo nó từ XAML không hoạt động.

Các vấn đề liên quan