WPF中的复杂模板继承

时间:2013-03-26 18:19:31

标签: wpf xaml templates inheritance styles

我创建了自己的按钮模板,其中有一个画布,里面定义了一些相互重叠的渐变。

基本按钮为灰色

<Style x:Key="ButtonGrey" TargetType="Button">
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="Width" Value="200" />
    <Setter Property="Height" Value="40" />
    <Setter Property="FontSize" Value="24" />
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="FontFamily" Value="Utsaah" />
    <Setter Property="Foreground" Value="#FFE6E7E8" />
    <Setter Property="Margin" Value="0,0,0,2" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">

                <Border x:Name="Border" 
                        BorderThickness="2"
                        Margin="-2"
                        BorderBrush="{x:Null}" >

                    <Canvas x:Name="ButtonGreyCanvas">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Pressed"/>
                                <VisualState x:Name="Disabled"/>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Focused"/>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="ValidationStates">
                                <VisualState x:Name="InvalidFocused"/>
                                <VisualState x:Name="InvalidUnfocused"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>

                    <!-- Button complex background -->
                        <Path x:Name="Bg1" Data="M0,40L200,40L200,0L0,0z" Stretch="Fill" Fill="{DynamicResource ButtonGreyBg1}" />
                        <Path x:Name="Bg2" Data="M0,40L200,40L200,0L0,0z" Stretch="Fill" Fill="{DynamicResource ButtonGreyBg2}" />
                        <!-- middle high dark strip -->
                        <Path x:Name="Bg3" Data="F1M200,20L0,20L0,40L200,40z" Fill="{DynamicResource ButtonGreyStripBg3}" Opacity="0.30000299215316772" Stretch="Fill" Canvas.Top="20" />

                        <ContentPresenter 
                            Content="{TemplateBinding Content}"
                            Margin="40,8,0,0"/>

                        <ContentPresenter 
                            Content="{TemplateBinding Content}"
                            TextBlock.Foreground="#1EE6E7E8"
                            Margin="41.5,6.8,0,0"/>                     
                    </Canvas>
                </Border>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="Foreground" Value="White" />
                        <Setter TargetName="Border" Property="BorderThickness" Value="1.3" />
                        <Setter TargetName="Border" Property="Margin" Value="-1.3" />
                        <Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource ButtonGreyOverBrush}"/>
                    </Trigger>
                    <Trigger Property="IsDefaulted" Value="True"/>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="Border" Property="BorderThickness" Value="2" />
                        <Setter TargetName="Border" Property="Margin" Value="-2" /> 
                        <Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource ButtonGreyOverBrush}"/>                          
                        <Setter Property="Foreground" Value="White" />
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Foreground" Value="White" />
                        <Setter TargetName="Border" Property="BorderThickness" Value="2" />
                        <Setter TargetName="Border" Property="Margin" Value="-2" />
                        <Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource ButtonGreyOverBrush}"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False"/>
                </ControlTemplate.Triggers>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

现在我只想将Bg1,Bg2和Bg3路径更改为不同的gradientBrush,因此我创建了继承自ButtonGrey的新样式(称为ButtonBlue),但我无法简单地访问这些路径(Bg1,Bg2)和Bg3)直接相似,如

<Style x:Key="ButtonBlue" TargetType="Button" BasedOn="{StaticResource ButtonGrey}">
    <Setter TargetName="Bg1" ...???...  />
</Style>

任何人都可以帮助如何在画布上访问这些路径吗?

非常感谢!

0 个答案:

没有答案