【问题标题】:Change property of "super/base" style wpf更改“超级/基本”样式 wpf 的属性
【发布时间】:2011-09-26 21:36:28
【问题描述】:

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

假设我有这种基本风格:

        <!-- 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" />
                                     ....
                                     ...

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

【问题讨论】:

    标签: wpf xaml inheritance styles basedon


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2019-09-08
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多