I have an ellipse object in wpf. Upon certain trigger I launch an storyboard animation shown below
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="outterEllipse">
<EasingColorKeyFrame KeyTime="0" Value="{DynamicResource MainBlueColor}">
<EasingColorKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseIn"/>
</EasingColorKeyFrame.EasingFunction>
</EasingColorKeyFrame>
<EasingColorKeyFrame KeyTime="0:0:0.5" Value="White"/>
</ColorAnimationUsingKeyFrames>
My ellipse definition is
<Ellipse x:Name="outterEllipse" Fill="{DynamicResource MainBlueSolidColorBrush}">
Where MainBlueSolidColorBrush is a brush defined somewhere.
The brush
<SolidColorBrush Color="{DynamicResource MainBlueColor}" x:Key="BlueSolidColorBrush" x:Name="BlueSolidColorBrush" ></SolidColorBrush>
The problem
When the storyboard runs other elements that have the same brush change as well. It seems like the animation is changing the resource and not the value for that object only. Is there any solution to stop this happening?
It seems like the animation is changing the resource and not the value for that object only.
By default, when you declare a resource, that object is created only once, and shared in every place where the resource is referenced. So in your example, each element that uses that resource refers to the same object.
Is there any solution to this?
Without a good Minimal, Complete, and Verifiable code example, it's impossible to know for sure what is needed to fix your issue.
The sharing behavior can be modified. You can add x:Shared="false" to the resource declaration. This will tell WPF that each reference to the resource should get its own copy of the declared resource, instead of sharing it with other referencing elements.
Based on the code you posted, I would expect this to work in your case. E.g. change the resource declaration to something like:
<SolidColorBrush x:Key="MainBlueSolidColorBrush" x:Shared="false" ... />
If that doesn't address your scenario, please improve your question by providing a good MCVE that reliably reproduces the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With