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

发布于 2024-12-20 07:14:56 字数 2899 浏览 1 评论 0原文

我正在开发一个源自列表框的自定义控件。作为我的控件的一部分,我想提供一个默认的 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,并且没有显示绑定错误。

有想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明月夜 2024-12-27 07:14:56

事实证明我缺少 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 绑定,因此它显然从未被调用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文