Yes, you can apply the container parameter to FrameworkElement and call FindResource to search for resources starting with ContentPresenter . For instance:
Code:
public class MySelector : DataTemplateSelector { public override DataTemplate SelectTemplate (object item, DependencyObject container) {
XAML:
<UserControl x:Class="WpfApplication1.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication1" > <UserControl.Resources> <DataTemplate x:Key="one"> <TextBlock>Template One</TextBlock> </DataTemplate> <DataTemplate x:Key="two"> <TextBlock>Template Two</TextBlock> </DataTemplate> <local:MySelector x:Key="MySelector"/> </UserControl.Resources> <StackPanel> <ContentPresenter ContentTemplateSelector="{StaticResource MySelector}" Content="a"/> <ContentPresenter ContentTemplateSelector="{StaticResource MySelector}" Content="b"/> </StackPanel> </UserControl>
source share