Search code examples
wpfdatatemplateselector

Applying a DataTemplateSelector in a Custom Control Style


I am developing a custom control which descends from a ListBox. As part of my control, I want to provide a default ItemTemplateSelector. Unfortunately, I can't see to get it to find my DataTemplateSelector. Here is the code, with irrelevant details elided:

public class AnnotationTemplateSelector : DataTemplateSelector
{
    public DataTemplate BoxValuePairTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        return BoxValuePairTemplate;
    }
}

And the Xaml...

    <DataTemplate x:Key="BoxValuePairDataTemplate">
        <Grid TextBlock.FontSize="9" Height="13" Width="{Binding Path=Width}" SnapsToDevicePixels="False">
     ... etc...
        </Grid>                                  
     </DataTemplate>

The selector:

    <Annotations:AnnotationTemplateSelector BoxValuePairTemplate="{StaticResource BoxValuePairDataTemplate}"
                                            x:Key="AnnotationTemplateSelector" />

The style:

    <Style TargetType="{x:Type Annotations:BoxEditorSurface}">
        <Setter Property="IsTabStop" Value="False"/>
        <Setter Property="ItemTemplateSelector" Value="{StaticResource AnnotationTemplateSelector}"/>
        ... etc ...
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Canvas.Left" Value="{Binding Path=X, Mode=TwoWay}" />
                <Setter Property="Canvas.Top" Value="{Binding Path=Y, Mode=TwoWay}" />
                <Setter Property="Padding" Value="0" />
                <Setter Property="IsTabStop" Value="False" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <ContentPresenter x:Name="contentPresenter"
                                              Content="{TemplateBinding Content}"
                                              ContentTemplate="{TemplateBinding ContentTemplate}"
                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              Margin="{TemplateBinding Padding}"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>

                <Style.Triggers>
                    <Trigger Property="IsKeyboardFocusWithin" Value="True">
                        <Setter Property="IsSelected" Value="True" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Setter.Value>
    </Setter>

    </Style>

Unfortunately, after the template is applied, the ItemTemplateSelector is always null, and no binding errors are shown.

Ideas?


Solution

  • It turns out I was missing a binding in the ContentPresenter. I'm providing my answer now in hopes it will prevent frustration for whomever might make the same mistake in future:

        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Canvas.Left" Value="{Binding Path=X, Mode=TwoWay}" />
                    <Setter Property="Canvas.Top" Value="{Binding Path=Y, Mode=TwoWay}" />
                    <Setter Property="Padding" Value="0" />
                    <Setter Property="IsTabStop" Value="False" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListBoxItem">
                                <ContentPresenter x:Name="contentPresenter"
                                                  Content="{TemplateBinding Content}"
                                                  ContentTemplate="{TemplateBinding ContentTemplate}"
                                                  ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
                                                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                  Margin="{TemplateBinding Padding}"/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
    
                    <Style.Triggers>
                        <Trigger Property="IsKeyboardFocusWithin" Value="True">
                            <Setter Property="IsSelected" Value="True" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Setter.Value>
        </Setter>
    

    I was missing the ContentTemplateSelector Binding in the ContentPresenter, so it was obviously never invoked.