更改""的属性;超级/基础";风格wpf



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

假设我有这种基本风格:

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

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

您可以创建一个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被覆盖,这会导致模板也发生更改,因为它引用了此资源。

相关内容

  • 没有找到相关文章

最新更新