WPF UserControls: Image disappears even with 'x:Shared="False"'
The problem is not the BitmapImage
it's the content in the setter of the button - it's created once and thus has to "jump" between instances.
The easy, but not WPF-isque, solution is setting x:Shared="False"
on the style.
The correct way is to use ControlTemplate
or DataTemplate
.
From your observations:
"if I set the content of both buttons to the image directly it works ok" - this is because to do that you create two different instances of Image
object.
But, this: "the whole point of styling is to avoid exactly that!" is a misconception - styles are not meant to set the content of content controls, the content is dependent upon the context. If you have a visual that is repeating for all buttons (without dependency upon the content), it should reside in ControlTemplate
for the button. If you have a visual that is dependent upon the content (but the content is not the visual) it should reside in DataTemplate
.