在自定义控件样式中应用DataTemplateSelector

时间:2011-12-07 03:19:36

标签: wpf datatemplateselector

我正在开发一个来自ListBox的自定义控件。作为我控制的一部分,我想提供一个默认的ItemTemplateSelector。不幸的是,我无法让它找到我的DataTemplateSelector。以下是代码,其中包含无关的详细信息:

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

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

和Xaml ......

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

选择器:

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

风格:

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

不幸的是,在应用模板之后,ItemTemplateSelector始终为null,并且不显示绑定错误。

想法?

1 个答案:

答案 0 :(得分:2)

事实证明我在ContentPresenter中缺少绑定。我现在正在提供我的答案,希望它可以防止将来可能犯同样错误的人感到沮丧:

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

我在ContentPresenter中缺少ContentTemplateSelector Binding,因此显然从未调用它。