5
votes

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>
1

1 Answers

7
votes

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>