1

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

假设我有这种基本风格:

        <!-- 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 相同的属性,但图像从 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" />
                                     ....
                                     ...

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

4

1 回答 1

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>

InStyleBTextBrush覆盖,这导致模板也发生变化,因为它引用了这个资源。

于 2011-07-01T16:56:06.523 回答