是否可以引用Microsoft程序集中定义的控制模板?
时间:2020-03-05 18:55:08 来源:igfitidea点击:
我有必须为几个WPF控件(即GridViewHeader)提供自己的控件模板的方案。当我们混合查看GridViewHEader的控件模板时,它是与其他几个控件聚合而成的,在某些情况下,这些控件仅针对该控件设置样式,即此列之间的分隔符。
这些模板显然是隐藏在system ... dll(或者主题dll中的某个位置)中的资源。
所以,我的问题是有没有办法引用那些预定义的模板?到目前为止,我最终在我的资源中拥有了自己的副本,但是我不喜欢这种方法。
这是示例方案:
我有一个GridViewColumnHeader:
<Style TargetType="{x:Type GridViewColumnHeader}" x:Key="gridViewColumnStyle"> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> <Setter Property="VerticalContentAlignment" Value="Stretch"/> <Setter Property="Background" Value="{StaticResource GridViewHeaderBackgroundColor}"/> <Setter Property="BorderBrush" Value="{StaticResource GridViewHeaderForegroundColor}"/> <Setter Property="BorderThickness" Value="0"/> <Setter Property="Padding" Value="2,0,2,0"/> <Setter Property="Foreground" Value="{StaticResource GridViewHeaderForegroundColor}"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type GridViewColumnHeader}"> <Grid SnapsToDevicePixels="true" Tag="Header" Name="Header"> <ContentPresenter Name="HeaderContent" Margin="0,0,0,1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> <Canvas> <Thumb x:Name="PART_HeaderGripper" Style="{StaticResource GridViewColumnHeaderGripper}"/> </Canvas> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="true"> </Trigger> <Trigger Property="IsPressed" Value="true"> <Setter TargetName="HeaderContent" Property="Margin" Value="1,1,0,0"/> </Trigger> <Trigger Property="Height" Value="Auto"> <Setter Property="MinHeight" Value="20"/> </Trigger> <Trigger Property="IsEnabled" Value="false"> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
到目前为止,没有什么有趣的,但是,我想直接在模板中添加一些额外的功能,而无需保留简洁的演示者,在其旁边添加控件,并且我想让Thumb保留框架的默认设置。我在这里找到了Microsoft提供的主题:
Thumb的主题如下所示:
<Style x:Key="GridViewColumnHeaderGripper" TargetType="{x:Type Thumb}"> <Setter Property="Canvas.Right" Value="-9"/> <Setter Property="Width" Value="18"/> <Setter Property="Height" Value="{Binding Path=ActualHeight,RelativeSource={RelativeSource TemplatedParent}}"/> <Setter Property="Padding" Value="0"/> <Setter Property="Background" Value="{StaticResource GridViewColumnHeaderBorderBackground}"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Thumb}"> <Border Padding="{TemplateBinding Padding}" Background="Transparent"> <Rectangle HorizontalAlignment="Center" Width="1" Fill="{TemplateBinding Background}"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>
到目前为止,我必须复制并粘贴该样式,而我希望从资源中引用它。
解决方案
回答
引用100%可能会更改的内部资源仅复制它就更好了。
回答
可以引用它们,但是正如paulbetts所说,不建议这样做,因为它们可能会更改。还请考虑我们所做的事情是否确实是"正确的"。我们可以编辑问题来解释为什么需要这样做吗?