隐式DataTemplate不起作用 [英] Implicit DataTemplate doesn't work

查看:56
本文介绍了隐式DataTemplate不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下隐式 DataTemplate 不起作用?只有注释的内联 DataTemplate 起作用。

Why does the following implicit DataTemplate not work? Only the commented inline DataTemplate will work.

注意:如果我同时删除了 DataTemplate s,我看到 ProductListView 完整类型名称的字符串表示形式。

Note: If I remove both DataTemplates, I see a string representation of the ProductListView full type name.

<Window.Resources>
  <DataTemplate DataType="vm:ProductListViewModel">
    <v:ProductListView/>
  </DataTemplate>
</Window.Resources>

<TabControl ItemsSource="{Binding Tabs}" TabStripPlacement="Left">
  <TabControl.ItemTemplate>     
    <DataTemplate>
      <TextBlock Text="{Binding Key}"/>
    </DataTemplate>        
  </TabControl.ItemTemplate>
  <TabControl.ContentTemplate>
    <DataTemplate>
      <ContentPresenter Content="{Binding Value}">

        <!--ContentPresenter.ContentTemplate>
          <DataTemplate DataType="vm:ProductListViewModel">
            <v:ProductListView/>
          </DataTemplate>
        </ContentPresenter.ContentTemplate-->

      </ContentPresenter>
    </DataTemplate>
  </TabControl.ContentTemplate>
</TabControl>


推荐答案

DataType 要求使用 x:Type ,因为属性的类型为 Object ,因此,如果键入 DataType = ns:Type ,将其设置为字符串 ns:Type 。如果属性的类型为 Type (如 Style.TargetType 例如),XAML处理器将自动转换该字符串转换为 Type

DataType requires the use of x:Type as the property's type is Object, so if you type DataType="ns:Type" you set it to the string "ns:Type". If the property's type were Type (as with Style.TargetType for example) the XAML processor would automatically convert that string to a Type.

因此,您应该在此处输入:

Thus here you should write:

  <DataTemplate DataType="{x:Type vm:ProductListViewModel}">
    <v:ProductListView/>
  </DataTemplate>

(属性类型为 Object 允许对XML数据进行数据模板化,请参见文档以获取更多信息)

(The property type is Object to allow data-templating of XML data, see the documentation for more information on that)

这篇关于隐式DataTemplate不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆