0

I have several image styles in my resource dictionary. As you can see, the alignment properties are duplicated so I'd like to bring these out to another style so their values are not duplicated. Is there a way to use linked styles in resource dictionary? and how can I do this?

<Style TargetType="Image" x:Key="Blank">
    <Setter Property="Source" Value="/images/blank.png"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
</Style>

<Style TargetType="Image" x:Key="BlockArrowLeft">
    <Setter Property="Source" Value="/images/block_arrow_left.png"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
</Style>

1 Answer 1

1

Of course. You can use inheritance in styles:

<Style TargetType="Image" x:Key="common">
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
</Style>

<Style TargetType="Image" x:Key="Blank" BasedOn="{StaticResource common}">
    <Setter Property="Source" Value="/images/blank.png"/>
</Style>

<Style TargetType="Image" x:Key="BlockArrowLeft" BasedOn="{StaticResource common}">
    <Setter Property="Source" Value="/images/block_arrow_left.png"/>
</Style>

Or:

<Style TargetType="Image">
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
</Style>

<Style TargetType="Image" x:Key="Blank" BasedOn="{StaticResource {x:Type Image}}">
    <Setter Property="Source" Value="/images/blank.png"/>
</Style>

<Style TargetType="Image" x:Key="BlockArrowLeft" BasedOn="{StaticResource {x:Type Image}}">
    <Setter Property="Source" Value="/images/block_arrow_left.png"/>
</Style>
2
  • Great. Exactly what I was looking for. Thanks. -Tom
    – namg_engr
    Commented Jan 1, 2018 at 23:06
  • @namg_engr glad to help. don't forget to mark my post as an answer
    – Bizhan
    Commented Jan 1, 2018 at 23:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.