代码之家  ›  专栏  ›  技术社区  ›  dex3703

wpf controltemplates是否必须具有targetType?

  •  10
  • dex3703  · 技术社区  · 15 年前

    WPF中的ControlTemplates是否需要TargetType?我正在重新建立一些控件的样式,注意到ComboBoxitem、ListiviewItem和ListBoxItem都具有相同的模板:

        <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必须具有类型的特定引用。

    2 回复  |  直到 14 年前
        1
  •  15
  •   John Bowen    15 年前

    没有对TargetType的要求,但是如果不指定TargetType,它的行为将与指定TargetType控件的行为相同。指定类型给您的主要好处是,无需使用所有者类型限定属性,即可访问模板绑定和触发器等类型的所有依赖属性。如果没有TargetType,也可能丢失隐式绑定,如ContentPresenter到ContentControl.Content属性。一旦指定了TargetType,模板就只能应用于该类型的控件或从该类型派生。要在不同类型之间共享,只需在本例中指定一个公共的基类ContentControl。

    以下简单模板将给出相同的基本结果,但第一个模板更可取,更常见:

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

    如果没有类型,所有内容属性都需要手动连接:

    <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>
    
        2
  •  2
  •   Femaref    15 年前

    它们都来自 System.Windows.Controls.ContentControl 所以你可以把它作为目标。