WPF CONTROLTEMPLATES必须具备的TargetType或不? [英] WPF ControlTemplates must have TargetType or not?

查看:354
本文介绍了WPF CONTROLTEMPLATES必须具备的TargetType或不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不要在WPF CONTROLTEMPLATES需要一个TargetType的?我改型一些控制,并注意comboboxitem,listiviewitem和ListBoxItem中都具有相同的模板:

Do ControlTemplates in WPF require a TargetType? I am restyling some controls, and notice that the comboboxitem, listiviewitem and listboxitem all have the same template:

    <ControlTemplate x:Key="ListBoxItemCT" TargetType="{x:Type ListBoxItem}">

    <Border x:Name="Bd" 
        SnapsToDevicePixels="true" 
        Background="{TemplateBinding Background}" 
        BorderBrush="{TemplateBinding BorderBrush}" 
        BorderThickness="{TemplateBinding BorderThickness}" 
        Padding="{TemplateBinding Padding}"
        CornerRadius="1">
        <ContentPresenter x:Name="cpItemContent"
            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
            VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
            />
    </Border>

</ControlTemplate>

是否有可能只是删除的TargetType,并有一个模板为所有三个?我想这样做,但得到奇怪的错误和问题。我找不到任何具体的引用,CONTROLTEMPLATES必须有一个类型。

Is it possible to just remove the TargetType and have one template for all three? I'm trying to do this but get strange errors and problems. I can't find any specific reference that ControlTemplates must have a type.

推荐答案

没有一个TargetType的要求,但如果你没有指定它的行为一样的,如果你指定控制的TargetType的。其主要优点是指定类型给你的是访问的东西像TemplateBindings和触发器所有类型的依赖属性,而不必进入决赛所有者类型的财产。如果没有你的TargetType也可以输隐含的绑定,如内容presenter到ContentControl.Content财产。一旦你指定的模板可以只适用于该类型的控件或从该类型派生的TargetType的。不同类型之间共享只需指定一个共同的基类 - 在这种情况下ContentControl中

There isn't a requirement for a TargetType, but if you don't specify one it will behave the same as if you specify a TargetType of Control. The main advantage that specifying a type gives you is access to all of that type's Dependency Properties in things like TemplateBindings and Triggers without having to qualify the property with the owner type. Without a TargetType you can also lose implicit bindings, like ContentPresenter to the ContentControl.Content property. Once you do specify a TargetType that template can only be applied to controls of that type or derived from that type. To share between different types just specify a common base class - ContentControl in this case.

下面这个简单的模板将给予相同的基本结果,但首先是preferable,更常见的:

The following simple templates will give the same basic result but the first is preferable and more common:

<ControlTemplate x:Key="CommonContentTemplate" TargetType="{x:Type ContentControl}">
    <Border x:Name="Bd" 
            SnapsToDevicePixels="true" 
            Background="{TemplateBinding Background}" 
            BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}" 
            Padding="{TemplateBinding Padding}"
            CornerRadius="1">
        <ContentPresenter x:Name="cpItemContent"
                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
    </Border>
</ControlTemplate>

如果没有所有的内容属性需要手工接线类型:

Without the type all of the Content properties need to be wired up manually:

<ControlTemplate x:Key="CommonTemplate">
    <Border x:Name="Bd" 
            SnapsToDevicePixels="true" 
            Background="{TemplateBinding Background}" 
            BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}" 
            Padding="{TemplateBinding Padding}"
            CornerRadius="1">
        <ContentPresenter x:Name="cpItemContent"
                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                          Content="{TemplateBinding ContentControl.Content}"
                          ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                          ContentTemplateSelector="{TemplateBinding ContentControl.ContentTemplateSelector}"
                          ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"/>
    </Border>
</ControlTemplate>

这篇关于WPF CONTROLTEMPLATES必须具备的TargetType或不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆