.net WPF ControlTemplates是否必须具有TargetType?
WPF中的ControlTemplates是否需要TargetType?我正在重新设置一些控件的样式,请注意comboboxitem、ListViveItem和listboxitem都具有相同的模板:.net WPF ControlTemplates是否必须具有TargetType?,.net,wpf,templates,controltemplate,targettype,.net,Wpf,Templates,Controltemplate,Targettype,WPF中的ControlTemplates是否需要TargetType?我正在重新设置一些控件的样式,请注意comboboxitem、ListViveItem和listboxitem都具有相同的模板: <ControlTemplate x:Key="ListBoxItemCT" TargetType="{x:Type ListBoxItem}"> <Border x:Name="Bd" SnapsToDevicePixels="true"
<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必须具有类型的任何特定引用。它们都源于
System.Windows.Controls.ContentControl
,因此您可以将其作为目标。对TargetType没有要求,但如果不指定一个,它的行为将与指定控件的TargetType相同
- 指定类型的主要优点是可以访问所有 在TemplateBindings之类的东西中使用该类型的依赖属性 并触发,而无需与所有者确认属性 类型
- 如果没有TargetType,也可能会丢失隐式绑定,如 ContentPresenter指向ContentControl.Content属性
<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>
为完整起见,请注意: 如果 模板定义包含ContentPresenter
虽然它没有解释这个要求,但很可能是由给出的推理,即您必须手动指定基本属性,如
Content
,否则这些属性将自动连接。谢谢!我花了两周的时间制作了这个大的依赖属性图,所以这很有意义。我想我可以试试…:)这就解释了我所犯的奇怪错误(关于找不到来自控件的东西)以及为什么内容不会出现。谢谢!与上述答案相同,但更简洁。:)