Implicit DataTemplate doesn't work
Asked Answered
H

1

11

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

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>
Hassett answered 13/11, 2011 at 2:25 Comment(0)
E
13

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>

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

Englut answered 13/11, 2011 at 3:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.