隐式DataTemplate不起作用

Shi*_*mmy 10 wpf datatemplate

为什么以下隐含DataTemplate不起作用?只有注释的内联才DataTemplate有效.

注意:如果我删除了两个DataTemplates,我会看到ProductListView完整类型名称的字符串表示.

<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>
Run Code Online (Sandbox Code Playgroud)

H.B*_*.B. 12

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

因此,你应该写:

  <DataTemplate DataType="{x:Type vm:ProductListViewModel}">
    <v:ProductListView/>
  </DataTemplate>
Run Code Online (Sandbox Code Playgroud)

(属性类型Object允许对XML数据进行数据模板化,有关详细信息,请参阅文档)