Styles in WPF
-
Hi, I have a simple code snippet that changes the color of a background of a boder using animation. How to define that in style? I am getting the error message saying that I cannot specificy TargetName.
<Border.Background> <SolidColorBrush x:Name="brush"></SolidColorBrush> </Border.Background> <Border.Triggers> <EventTrigger RoutedEvent="Border.MouseEnter"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <ColorAnimation Storyboard.TargetName="brush" Storyboard.TargetProperty="Color" To="Yellow" Duration="0:0:0.5"></ColorAnimation> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> <EventTrigger RoutedEvent="Border.MouseLeave"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <ColorAnimation Storyboard.TargetName="brush" Storyboard.TargetProperty="Color" To="Transparent" Duration="0:0:0.5"></ColorAnimation> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> </Border.Triggers>
Thanks in advance for help -
Hi, I have a simple code snippet that changes the color of a background of a boder using animation. How to define that in style? I am getting the error message saying that I cannot specificy TargetName.
<Border.Background> <SolidColorBrush x:Name="brush"></SolidColorBrush> </Border.Background> <Border.Triggers> <EventTrigger RoutedEvent="Border.MouseEnter"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <ColorAnimation Storyboard.TargetName="brush" Storyboard.TargetProperty="Color" To="Yellow" Duration="0:0:0.5"></ColorAnimation> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> <EventTrigger RoutedEvent="Border.MouseLeave"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <ColorAnimation Storyboard.TargetName="brush" Storyboard.TargetProperty="Color" To="Transparent" Duration="0:0:0.5"></ColorAnimation> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> </Border.Triggers>
Thanks in advance for helpStyle looks like this: <setter property="Template"> <setter.value> <controltemplate targettype="{x:Type Button}"> <controltemplate.resources> <!-- Storyboards go here --> </controltemplate.resources> <grid> <!-- Content goes here --> </grid> <controltemplate.triggers> <!-- Triggers go here --> </controltemplate.triggers> </controltemplate> </setter.value> </setter> You have to put everything in the appropriate place for it to work, then apply the style to the element you want
-
Style looks like this: <setter property="Template"> <setter.value> <controltemplate targettype="{x:Type Button}"> <controltemplate.resources> <!-- Storyboards go here --> </controltemplate.resources> <grid> <!-- Content goes here --> </grid> <controltemplate.triggers> <!-- Triggers go here --> </controltemplate.triggers> </controltemplate> </setter.value> </setter> You have to put everything in the appropriate place for it to work, then apply the style to the element you want
-
Thanks for reply! Is it possible to do that without defining a template? I believe that a simple style with 2 triggers would be enough
-
I thought the same and tried it, but it didn`t work. I got an error like I wrote in first post.