是否可以引用microsoft程序集中定义的控件模板?

时间:2008-09-16 04:32:21

标签: wpf xaml

我有一个场景,我必须为几个WPF控件提供我自己的控件模板 - 即GridViewHeader。当您在混合中查看GridViewHEader的控件模板时,它会与其他几个控件混合使用,在某些情况下,这些控件仅针对该控件设置样式 - 即列之间的此拆分器。 那些模板,显然是隐藏在系统中的资源... 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>

到目前为止 - 没有什么有趣的,但是说,我想在模板中直接添加一些额外的功能 - 我会按原样保留cotnent演示者,在它旁边添加我的控件,我想留下Thumb的默认值来自框架。我找到了由microsoft here提供的主题:

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>

到目前为止 - 我必须复制&amp;粘贴那种风格,而我更愿意从资源中引用它。

2 个答案:

答案 0 :(得分:2)

引用100%可更改的内部资源是不可维护的 - 最好只复制它。

答案 1 :(得分:0)

可以参考它们,但正如paulbetts所说,不推荐它,因为它们可以改变。还要考虑你所做的事情是否真的“正确”。你能编辑你的问题来解释为什么你需要这样做吗?