title: How to use datatrigger for rectangle in a usercontrol in c# wpf?
body: <p>I created a UserControl with a complex rectangle in wpf c#, and I want to trigger the animation with a datatrigger which is boud to a property. If I use eventtrigger within the rectangle, it can work as expected, but if I switch to datatrigger, it failed to animate the rectangle...
  For the animation, credit @Val who posted <a href="https://stackoverflow.com/questions/48495674/wpf-moving-arrow-with-direction-change">WPF moving arrow with direction change</a></p>
  
  <ol>
  <li>I've tried including the storyboard within &lt; Rectangle.Resource>, and use _storyboard.begin() in code, but failed animate it.</li>
  <li>I've tried including the storyboard inside a  which contains a datatrigger that bound to a property named AnimationStart. And also I tried putting the &lt; Style> both inside &lt; UserControl.Resource> and &lt; Rectangle.Resource>, but neither worked.</li>
  <li>I also implemented either INotifyPropertyChanged or DependencyProperty for AnimationStart, but still none of them worked.</li>
  <li>the DependencyProperty: IndicationPoints, LineBackgroundColor, LineBackColor works properly.</li>
  </ol>
  
  <p>now the xaml is as follows</p>
  
  <pre><code> &lt;UserControl.Resources&gt;
          &lt;Style x:Key="animationStyle" TargetType="{x:Type Rectangle}"&gt;
              &lt;Style.Triggers&gt;
                  &lt;DataTrigger Binding="{Binding AnimationStart}" Value="Start"&gt;
                      &lt;DataTrigger.EnterActions&gt;
                          &lt;BeginStoryboard x:Name="StartAnimation"&gt;
                              &lt;Storyboard&gt;
                                  &lt;DoubleAnimation 
                                      From="0" 
                                      To="10" 
                                      RepeatBehavior="Forever"
                                      Storyboard.TargetProperty="(Rectangle.Fill).(VisualBrush.Transform).(TranslateTransform.X)" 
                                      Duration="0:0:0.1" /&gt;
                              &lt;/Storyboard&gt;
                          &lt;/BeginStoryboard&gt;
                      &lt;/DataTrigger.EnterActions&gt;
                  &lt;/DataTrigger&gt;
                  &lt;DataTrigger Binding="{Binding AnimationStart}" Value="Stop"&gt;
                      &lt;DataTrigger.EnterActions&gt;
                          &lt;StopStoryboard x:Name="StopAnimation" BeginStoryboardName="StartAnimation"/&gt;
                      &lt;/DataTrigger.EnterActions&gt;
                  &lt;/DataTrigger&gt;
              &lt;/Style.Triggers&gt;
          &lt;/Style&gt;
      &lt;/UserControl.Resources&gt;
  
      &lt;Rectangle x:Name="rectangleLine" Style="{StaticResource animationStyle}" Width="auto" Height="auto" Margin="0"&gt;
          &lt;Rectangle.RenderTransform&gt;
              &lt;RotateTransform Angle="0" /&gt;
          &lt;/Rectangle.RenderTransform&gt;
          &lt;Rectangle.Fill&gt;
              &lt;VisualBrush TileMode="Tile" Viewport="0,0,10,8" ViewportUnits="Absolute" Viewbox="0,0,8,8" ViewboxUnits="Absolute"&gt;
                  &lt;VisualBrush.Transform&gt;
                      &lt;TranslateTransform X="0" Y="0" /&gt;
                  &lt;/VisualBrush.Transform&gt;
                  &lt;VisualBrush.Visual&gt;
                      &lt;Grid x:Name="gridMoving"&gt;
                          &lt;Polygon x:Name="polygonMovingBack" Fill="{Binding LineBackColor}" Points="0,0 8,0 8,8 0,8" /&gt;
                          &lt;Polygon x:Name="polygonMoving" Fill="{Binding LineBackgroundColor}" Points="{Binding IndicationShape}" /&gt;
                      &lt;/Grid&gt;
                  &lt;/VisualBrush.Visual&gt;
              &lt;/VisualBrush&gt;
          &lt;/Rectangle.Fill&gt;
          &lt;Rectangle.Resources&gt;
              &lt;Storyboard x:Key="startAnimation"&gt;
                  &lt;DoubleAnimation 
                      From="0" 
                      To="10" 
                      RepeatBehavior="Forever"
                      Storyboard.TargetProperty="(Rectangle.Fill).(VisualBrush.Transform).(TranslateTransform.X)" 
                      Duration="0:0:0.1" /&gt;
              &lt;/Storyboard&gt;
          &lt;/Rectangle.Resources&gt;
  
          &lt;!--&lt;Rectangle.Triggers&gt;
              &lt;EventTrigger RoutedEvent="Control.Loaded"&gt;
                  &lt;EventTrigger.Actions&gt;
                      &lt;BeginStoryboard&gt;
                          &lt;Storyboard&gt;
                              &lt;DoubleAnimation 
                                  From="0" 
                                  To="10" 
                                  RepeatBehavior="Forever"
                                  Storyboard.TargetProperty="(Rectangle.Fill).(VisualBrush.Transform).(TranslateTransform.X)" 
                                  Duration="0:0:0.1" /&gt;
                          &lt;/Storyboard&gt;
                      &lt;/BeginStoryboard&gt;
                  &lt;/EventTrigger.Actions&gt;
              &lt;/EventTrigger&gt;
          &lt;/Rectangle.Triggers&gt;--&gt;
      &lt;/Rectangle&gt;
  </code></pre>
  
  <p>and the code behind</p>
  
  <pre><code>
          public PointCollection IndicationPoints
          {
              get
              {
                  return (PointCollection)GetValue(IndicationPointsProperty);
              }
              set
              {
                  SetValue(IndicationPointsProperty, value);
              }
          }
          public SolidColorBrush LineBackgroundColor
          {
              get
              {
                  return (SolidColorBrush)GetValue(LineBackgroundColorProperty);
              }
              set
              {
                  SetValue(LineBackgroundColorProperty, value);
              }
          }
          public SolidColorBrush LineBackColor
          {
              get
              {
                  return (SolidColorBrush)GetValue(LineBackColorProperty);
              }
              set
              {
                  SetValue(LineBackColorProperty, value);
              }
          }
          public string AnimationStart {
              get =&gt; _animationStart;
              set {
                  _animationStart = value;
                  NotifyPropertyChanged("AnimationStart");
              } }
  
          private string _animationStart;
  
          public event PropertyChangedEventHandler PropertyChanged;
          private void NotifyPropertyChanged(string info)
          {
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs(info));
              }
          }
  
          public static readonly DependencyProperty IndicationPointsProperty
              = DependencyProperty.Register("IndicationPoints", typeof(PointCollection), typeof(UI_MovingLine), new PropertyMetadata(new PointCollection(new List&lt;Point&gt; { new Point(0,0), new Point(4, 0), new Point(8, 4), new Point(4, 8), new Point(0, 8), new Point(4, 4) }), new PropertyChangedCallback(OnIndicationPointsChanged)));
  
          public static readonly DependencyProperty LineBackgroundColorProperty
              = DependencyProperty.Register("LineBackgroundColor", typeof(SolidColorBrush), typeof(UI_MovingLine), new PropertyMetadata(new SolidColorBrush(Colors.Gray), new PropertyChangedCallback(OnLineBackgroundColorChanged)));
  
          public static readonly DependencyProperty LineBackColorProperty
              = DependencyProperty.Register("LineBackColor", typeof(SolidColorBrush), typeof(UI_MovingLine), new PropertyMetadata(new SolidColorBrush(Colors.Gray), new PropertyChangedCallback(OnLineBackColorChanged)));
  
          private static void OnIndicationPointsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
          {
              var control = d as UI_MovingLine;
              control.OnIndicationPointsChanged(e);
          }
  
          private void OnIndicationPointsChanged(DependencyPropertyChangedEventArgs e)
          {
              polygonMoving.Points = (PointCollection)e.NewValue;
          }
  
          private static void OnLineBackgroundColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
          {
              var control = d as UI_MovingLine;
              control.OnLineBackgroundColorChanged(e);
          }
  
          private void OnLineBackgroundColorChanged(DependencyPropertyChangedEventArgs e)
          {
              polygonMoving.Fill = (SolidColorBrush)e.NewValue;
          }
  
          private static void OnLineBackColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
          {
              var control = d as UI_MovingLine;
              control.OnLineBackColorChanged(e);
          }
  
          private void OnLineBackColorChanged(DependencyPropertyChangedEventArgs e)
          {
              polygonMovingBack.Fill = (SolidColorBrush)e.NewValue;
          }
  
          public UI_MovingLine()
          {
              InitializeComponent();
              gridMoving.DataContext = this;
              polygonMoving.DataContext = this;
              LineMovingToNomal();
          }
  
          public void LineMovingToNomal()
          {
              try
              {
                  _transmitLineStatus = PowerSystem.TransmitLineStatus.TLS_NORMAL;
                  IndicationPoints = new PointCollection(new List&lt;Point&gt; { new Point(0, 0), new Point(4, 0), new Point(8, 4), new Point(4, 8), new Point(0, 8), new Point(4, 4) });
                  LineBackgroundColor = new SolidColorBrush(Colors.DodgerBlue);
                  LineBackColor = new SolidColorBrush(Colors.White);
  
                  AnimationStart = "Start";
              }
              catch
              {
              }
          }
  </code></pre>