There is a radiobutton with a custom style, in which there is a rectangle defined through the XAML style named rectangle . I want to make it so that when you hover the mouse on the radiobutton , the rectangle smoothly changes its color, but only if the radiobutton not selected ( IsChecked = False ). I use code

 <ControlTemplate.Triggers> <EventTrigger RoutedEvent="MouseEnter"> <BeginStoryboard> <Storyboard> <ColorAnimation Storyboard.TargetName="rectangle" Duration="0:0:0:0.5" Storyboard.TargetProperty="(Fill).(Color)" To="#5d4037"> </ColorAnimation> </Storyboard> </BeginStoryboard> </EventTrigger> 

Perhaps, I should make check through the code (C #)? If so, how to do it?

I tried to do it through code

 ColorAnimation aRadioEnter = new ColorAnimation(); Color color1 = (Color)ColorConverter.ConvertFromString("##5d4037"); aRadioEnter.Duration = new Duration(TimeSpan.FromSeconds(1)); aRadioEnter.To = color1; // Storyboard Storyboard; Storyboard = new Storyboard(); Storyboard.Children.Add(aRadioEnter); Storyboard.SetTargetName(aRadioEnter, target.Name); 

but I don’t know how to specify a target (rectangle Rectangle ) via Storyboard.SetTargetProperty .

And in general, how to get access to the controls through the code? For example, if I need to change the fill color of the Rectangle .

  • Once again I ask: why do you need to inherit from RadioButton ? What do you want to achieve? - VladD
  • Why not? It was a bad idea, now I use the usual radiobutton with a custom style. However, the question remains. Just now the crux of the matter is how to run the animation through the code and how to add animation to your elements (which are in control) through the code. - Theonic

1 answer 1

You do not need code-behind. It worked for me:

 <ControlTemplate.Resources> <Storyboard x:Key="SB"> <ColorAnimation Storyboard.TargetName="rectangle" Duration="0:0:0.5" Storyboard.TargetProperty="(Fill).(Color)" To="#5d4037"/> </Storyboard> </ControlTemplate.Resources> <ControlTemplate.Triggers> <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True"/> <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}" Value="True"/> </MultiDataTrigger.Conditions> <MultiDataTrigger.EnterActions> <BeginStoryboard Name="BeginMouseEnterSB" Storyboard="{StaticResource SB}"/> </MultiDataTrigger.EnterActions> <MultiDataTrigger.ExitActions> <RemoveStoryboard BeginStoryboardName="BeginMouseEnterSB"/> </MultiDataTrigger.ExitActions> </MultiDataTrigger> <!-- ... --> <ControlTemplate.Triggers> 

EventTrigger 'u can not set the condition, take DataTrigger (in our case - MultiDataTrigger ).