No DataType in DataTemplate in Windows Phone(—)

    之前WPF/SL用习惯了,现在研究Windows Phone一段时间,发现Windows Phone的SDK中做了许多限制,一些WPF/SL的方法属性都删掉了。

    今天在Windows Phone项目中用到了DataTemplate,用于Listbox的Items样式修改,但是测试时突然发现DataTemplate没有DataType属性。

如下:

<phone:PhoneApplicationPage.Resources>
        <DataTemplate x:Key="listDataTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding UserID}" />
                <TextBlock Text="  Name: "/>
                <TextBlock Text="{Binding UserName}" />
            </StackPanel>
        </DataTemplate>
</phone:PhoneApplicationPage.Resources>

使用<DataTemplate x:Key="listDataTemplate"> 可以,但是

<DataTemplate DataType="{x:Type local:UserList}">却没法用,之前在WPF中完全OK的,难道Windows Phone中把这项去掉了?

查看MSDN和百度,Windows Phone中DataTemplate貌似确实没有DataType的的属性,但是没有详细的说明,so,google之…(google在天朝被墙的太厉害了,很多网页打不开,而且速度超慢,残念…)

终于找到了英文的一些文章,Windows Phone中可能是为了性能和平台缘故,将DataType属性去掉了,而且更甚的是,DataTemplate中的Trigger也不给支持。

个人认为DataType和Trigger在绑定Itemcontrol的时候非常easy,性能或许会有损失,但是为了些许的损失去掉这么便捷的写法,真的值得么?

偶尔几个Control用Key值绑定DataTemplate 还好,但是如果是大量的Control,这样就非常繁琐。

OK,Windows Phone中与WPF的区别具体在哪,我们从代码出发查看一下,请看No DataType in DataTemplate in Windows Phone(二)

<phone:PhoneApplicationPage.Resources>
        <DataTemplate DataType="{x:Type local:UserList}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding UserID}" />
                <TextBlock Text="  Name: "/>
                <TextBlock Text="{Binding UserName}" />
            </StackPanel>
        </DataTemplate>
</phone:PhoneApplicationPage.Resources>
posted @ 2011-11-25 17:41  梦回千秋云断  阅读(519)  评论(0编辑  收藏  举报