Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I wrote a couple of simple Storyboards for highlighting an element in my UI:
- <Storyboard x:Key="highlightStoryboard" Storyboard.TargetProperty="Stroke.Color">
- <ColorAnimation To="#ff4444" Duration="0:0:0.2" />
- <ColorAnimation BeginTime="0:0:0.2" RepeatBehavior="Forever" AutoReverse="True" To="#ffcccc" Duration="0:0:0.5" />
- </Storyboard>
- <Storyboard x:Key="unlightStoryboard" Storyboard.TargetProperty="Stroke.Color">
- <ColorAnimation Duration="0:0:0.5" FillBehavior="Stop"/>
- </Storyboard>
- I use Styles and Triggers to apply these when the mouse is over an appropriate element:
- <Style TargetType ="{x:Type Rectangle}">
- <Style.Triggers>
- <Trigger Property="IsMouseOver" Value="True">
- <Trigger.EnterActions>
- <BeginStoryboard Storyboard="{StaticResource highlightStoryboard}"/>
- </Trigger.EnterActions>
- <Trigger.ExitActions>
- <BeginStoryboard Storyboard="{StaticResource unlightStoryboard}"/>
- </Trigger.ExitActions>
- </Trigger>
- </Style.Triggers>
- </Style>
- These are all defined in resources.
- This all works quite nicely. However, now I'd like to apply the same animation to a different element. Lets say a TextBox. However the Stroke.Color property is not appropriate. I need to apply that animation to the BorderBrush.Color property instead. For a different element again it would be Foreground.Color property...
- I was hoping I could move the Storyboard.TargetProperty down into the BeginStoryboard, but no dice - I get an exception at runtime complaining that the TargetProperty has not been set.
- For now I've duplicated the Storyboards for each target property, but that's horrible. My only other thought was to use an attached property which I can target in the animation, and then bind that to the actual property used per-object.
- Is there a better way of doing this?
Add Comment
Please, Sign In to add comment