Let's say I have a lot of button declarations in WPF. These button declarations are very similar and vary only by one property. Here's some example code:
<Button x:Name="Button1"
...>
<Image Source="{StaticResource ButtonImage}"
Style="{StaticResource ButtonImageStyle}" />
</Button>
<Button x:Name="Button2"
...>
<Image Source="{StaticResource ButtonImage}"
Style="{StaticResource ButtonImageStyle}" />
</Button>
<Button x:Name="Button3"
...>
<Image Source="{StaticResource ButtonImage}"
Style="{StaticResource ButtonImageStyle}" />
</Button>
<Button x:Name="Button4"
...>
<Image Source="{StaticResource ButtonImage}"
Style="{StaticResource ButtonImageStyle}" />
</Button>
<Button x:Name="Button5"
...>
<Image Source="{StaticResource ButtonImage}"
Style="{StaticResource ButtonImageStyle}" />
</Button>
All these buttons look the same, they just vary by their name.
Since all the properties of the button are the same (except for the name) I created a style containing all the properties that are the same and used that style in all of my buttons.
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="..." Value="..." />
</Style>
<Button x:Name="Button1"
Style="{StaticResource ButtonStyle">
<Image Source="{StaticResource ButtonImage}"
Style="{StaticResource ButtonImageStyle}" />
</Button>
<Button x:Name="Button2"
Style="{StaticResource ButtonStyle">
<Image Source="{StaticResource ButtonImage}"
Style="{StaticResource ButtonImageStyle}" />
</Button>
<Button x:Name="Button3"
Style="{StaticResource ButtonStyle">
<Image Source="{StaticResource ButtonImage}"
Style="{StaticResource ButtonImageStyle}" />
</Button>
<Button x:Name="Button4"
Style="{StaticResource ButtonStyle">
<Image Source="{StaticResource ButtonImage}"
Style="{StaticResource ButtonImageStyle}" />
</Button>
<Button x:Name="Button5"
Style="{StaticResource ButtonStyle">
<Image Source="{StaticResource ButtonImage}"
Style="{StaticResource ButtonImageStyle}" />
</Button>
Everything works fine so far.
Now, since even the image is the same on all the buttons, I wanted to move this image to the style as well.
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="..." Value="..." />
<Setter Property="Content">
<Setter.Value>
<Image Source="{StaticResource ButtonImage}"
Style="{StaticResource ButtonImageStyle}" />
</Setter.Value>
</Setter>
</Style>
<Button x:Name="Button1"
Style="{StaticResource ButtonStyle" />
<Button x:Name="Button2"
Style="{StaticResource ButtonStyle" />
<Button x:Name="Button3"
Style="{StaticResource ButtonStyle" />
<Button x:Name="Button4"
Style="{StaticResource ButtonStyle" />
<Button x:Name="Button5"
Style="{StaticResource ButtonStyle" />
But now I only see the image on the current active button, all the other buttons don't show the image. I guess with moving the image to the style it is only created in memory once and therefore it can be only shown once.
Is there a way to tell WPF, that an object created in a style should be created separately for every object that uses this style?