namg_engr
namg_engr

Reputation: 359

using linked styles in resource dictionary in wpf

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>

Upvotes: 0

Views: 33

Answers (1)

Bizhan
Bizhan

Reputation: 17145

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>

Upvotes: 1

Related Questions