【问题标题】:Is it possible to reference control templates defined in microsoft's assemblies?是否可以引用微软程序集中定义的控件模板?
【发布时间】:2008-09-16 04:32:20
【问题描述】:

我有一个场景,我必须为一些 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 Presenter 不变,在它旁边添加我的控件,我想留下 Thumb 的默认值框架。我找到了微软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>

到目前为止 - 我必须复制并粘贴该样式,而我更愿意从资源中获取对它的引用。

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2021-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-18
        • 2017-05-03
        相关资源
        最近更新 更多