【问题标题】:WPF ControlTemplates must have TargetType or not?WPF ControlTemplates 是否必须有 TargetType?
【发布时间】:2011-04-07 15:02:11
【问题描述】:

WPF 中的 ControlTemplate 是否需要 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 必须具有类型的特定参考。

【问题讨论】:

    标签: .net wpf templates controltemplate targettype


    【解决方案1】:

    对 TargetType 没有要求,但如果您不指定一个,它的行为将与您指定 Control 的 TargetType 相同。

    • 指定类型的主要优点是可以访问所有 该类型的依赖属性在诸如 TemplateBindings 之类的东西中 和触发器,而不必与所有者确认财产 输入。

    • 如果没有 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>
    

    【讨论】:

    • 谢谢!我花了过去两周制作这个大的依赖属性图,所以这是有道理的。我想我可以尝试... :)
    • 这可以解释我得到的奇怪错误(关于没有找到从 Control 派生的东西)以及为什么内容不会显示。
    【解决方案2】:

    它们都派生自 System.Windows.Controls.ContentControl,因此您可以改为定位它。

    【讨论】:

    • 谢谢!与上述相同的答案,但更简洁。 :)
    【解决方案3】:

    为了完整起见,请注意the documentation 声明如下:

    如果 ControlTemplate 上的 TargetType 属性是必需的 模板定义包含一个 ContentPresenter。

    虽然它没有对此要求作出解释,但很可能是 John Bowen's answer 给出的推理,您将不得不手动指定基本属性,如 Content,否则它们会自动连接。

    【讨论】:

      猜你喜欢
      • 2016-08-20
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 2020-01-18
      • 2015-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多