更改“超级/基础”样式wpf的属性

时间:2011-07-01 16:35:18

标签: wpf xaml inheritance styles basedon

我正在创建一个基本风格。我正在使用继承来实现我的基本样式。我知道如何在派生类上添加属性,但我不知道如何更改属性。让我举个例子:

假设我有这种基本风格:

        <!-- SrollViewer ScrollBar Repeat Buttons (at each end) -->
    <Style x:Key="ScrollBarLineButton" TargetType="{x:Type RepeatButton}">
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="Focusable" Value="false"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RepeatButton}">
                    <Border
                        Name="Border"
                        Margin="1"
                        CornerRadius="2"
                        Background="WhiteSmoke"          
                        BorderThickness="1">
                        <Image Stretch="Uniform" Source="/FilesPro;component/Images/scrollArrow.png" Height="40" VerticalAlignment="Center" HorizontalAlignment="Center" Width="52" >
                            <Image.RenderTransform>
                                <TransformGroup>
                                    <ScaleTransform ScaleY="-1" />
                                    <TranslateTransform Y="40"/>
                                    <SkewTransform/>
                                    <RotateTransform/>
                                    <TranslateTransform/>
                                </TransformGroup>
                            </Image.RenderTransform>
                        </Image>
                        <!--
                        <Path
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Fill="{StaticResource GlyphBrush}"
                            Data="{Binding Path=Content,
                                RelativeSource={RelativeSource TemplatedParent}}" >
                        </Path>
                        -->
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

    </Style>

我想创建一个新的样式,它具有与ScrollBarLineButton相同的属性,但是图像transfrom scaleY = 1而不是-1。

当我这样做时:

        <Style x:Key="ScrollBarLineButtonVerticalUp" BasedOn="{StaticResource ScrollBarLineButton}" TargetType="{x:Type RepeatButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RepeatButton}">
                    <Border>                            
                        <Image Stretch="Uniform" Source="/FilesPro;component/Images/scrollArrow.png" Height="40" VerticalAlignment="Center" HorizontalAlignment="Center" Width="52" >
                            <Image.RenderTransform>
                                <TransformGroup>
                                    <ScaleTransform ScaleY="1" />
                                     ....
                                     ...

边框不再有其基本样式边距,背景颜色等。我怎样才能继承样式并只更改一个属性。我知道我可以复制粘贴,但我会改变这一点很方便,如果我在一个地方改变它,它会在所有其他地方改变。

1 个答案:

答案 0 :(得分:6)

你可以创建一个DynamicResource引用来做这样的事情,这是一个例子:

<StackPanel>
    <StackPanel.Resources>
        <Style x:Key="ButtonStyleA" TargetType="{x:Type Button}">
            <Style.Resources>
                <SolidColorBrush x:Key="TextBrush" Color="Yellow" />
            </Style.Resources>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border CornerRadius="10" BorderThickness="1" BorderBrush="Red">
                            <ContentPresenter TextElement.Foreground="{DynamicResource TextBrush}"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="ButtonStyleB" TargetType="{x:Type Button}" BasedOn="{StaticResource ButtonStyleA}">
            <Style.Resources>
                <SolidColorBrush x:Key="TextBrush" Color="Blue" />
            </Style.Resources>
        </Style>
    </StackPanel.Resources>
    <Button Style="{StaticResource ButtonStyleA}" Content="Lorem Ipsum" />
    <Button Style="{StaticResource ButtonStyleB}" Content="Lorem Ipsum" />
</StackPanel>

StyleB中,TextBrush被覆盖,导致模板也会更改,因为它引用了此资源。