Can I use a DataTemplateSelector within a DataTemplate?
Asked Answered
C

1

6

I have an ItemsControl using a StackPanel to display a list of items.

I would like a label to appear for each row, but for the content to the left of the label to be defined by a DataTemplateSelector. I do not want to redefine the label for each DataTemplate generated by the TemplateSelector.

Is this possible?

<ItemsControl ItemsSource="{Binding Path=Values}" >
 <ItemsControl.Resources>
   <v:MyTemplateSelector x:Key="myTemplateSelector"></v:MyTemplateSelector>
 </ItemsControl.Resources>
 <ItemsControl.ItemsPanel>
  <ItemsPanelTemplate>
   <StackPanel></StackPanel>
  </ItemsPanelTemplate>
 </ItemsControl.ItemsPanel>
 <ItemsControl.ItemTemplate>
  <DataTemplate>
   <WrapPanel>
    <Label>Test: </Label>
    <!--What goes here should be defined by myTemplateSelector-->
   </WrapPanel>
  </DataTemplate>
 </ItemsControl.ItemTemplate>
</ItemsControl>
Coquette answered 31/12, 2010 at 3:39 Comment(0)
C
7

I figured it out. The solution was to use a ContentPresenter element with a ContentTemplateSelector attribute:

    <DataTemplate>
       <WrapPanel>
          <Label>Test: </Label>
          <ContentPresenter 
              ContentTemplateSelector="{StaticResource ResourceKey=myTemplateSelector}">
          </ContentPresenter>
       </WrapPanel>
    </DataTemplate>
Coquette answered 31/12, 2010 at 3:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.