Change property of "super/base" style wpf
I am creating a base style. I am using inheritance to implement my base style. I know how to add properties on my derived class but I don't know how to just change a property. Let me give you an example:
let say I have this base style:
<!-- 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/>
</TransformGrou开发者_如何学编程p>
</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>
I would like to create a new style that has the same properties as ScrollBarLineButton but with the image transfrom scaleY=1 instead of -1.
when I do:
<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 no longer has its base style margin, Background color etc.. How could I inherit from the style and just change one property. I know I can copy paste but I will change this a lot and it is convenient that if I change it in one place it changes in all the other places.
You can create a DynamicResource
reference to do something like this, here's an example:
<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>
In StyleB
the TextBrush
is overridden which causes the template to change as well since it references this resource.
精彩评论