2012-11-04 55 views
2

我正在C#中開發wpf應用程序。下面的xaml代碼對我來說工作正常。如何在C#中設置Storyboard.TargetProperty?

<VisualStateManager.VisualStateGroups> 
    <VisualStateGroup x:Name="VisualStateGroup"> 
     <VisualState x:Name="DefaultVisualState"> 
      <Storyboard/> 
     </VisualState> 
     <VisualState x:Name="FocusVisualState"> 
      <Storyboard> 
       <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="CircleEllipse1"> 
        <EasingColorKeyFrame KeyTime="0" Value="#FF0004FF"/> 
       </ColorAnimationUsingKeyFrames> 
       <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.Background).(SolidColorBrush.Color)" Storyboard.TargetName="TextBlock1"> 
        <EasingColorKeyFrame KeyTime="0" Value="#FFBCB0B0"/> 
       </ColorAnimationUsingKeyFrames> 
      </Storyboard> 
     </VisualState> 
    </VisualStateGroup> 
</VisualStateManager.VisualStateGroups> 

同樣的代碼,我用C#編寫

EasingColorKeyFrame easingColorKeyFrameObj = new EasingColorKeyFrame(); 
easingColorKeyFrameObj.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1)); 
easingColorKeyFrameObj.Value = Colors.Red; 
ColorAnimationUsingKeyFrames colorAnimationUsingKeyFramesObj = new ColorAnimationUsingKeyFrames(); 
colorAnimationUsingKeyFramesObj.KeyFrames.Add(easingColorKeyFrameObj); 
Storyboard.SetTargetName(colorAnimationUsingKeyFramesObj, "CircleEllipse1"); 
Storyboard.SetTargetProperty(
    colorAnimationUsingKeyFramesObj, new PropertyPath("Color")); 

Storyboard myStoryboard = new Storyboard(); 
myStoryboard.Children.Add(colorAnimationUsingKeyFramesObj); 

myStoryboard.Begin(this, true); 

在上面的代碼中,我應該怎麼設置Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"Storyboard.TargetProperty="(TextElement.Background).(SolidColorBrush.Color)"在C#

回答

1

你需要指定它爲一個字符串

private void AnimateColor(string ellipseName, Color ellipseColor, string textBlockName, Color textBlockColor) 
      { 
       EasingColorKeyFrame easingColorKeyFrameEllipseObj = new EasingColorKeyFrame(); 
       easingColorKeyFrameEllipseObj.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(12)); 
       easingColorKeyFrameEllipseObj.Value = ellipseColor; 

       ColorAnimationUsingKeyFrames colorAnimationUsingKeyFramesEllipseObj = new ColorAnimationUsingKeyFrames(); 
       Storyboard.SetTargetName(colorAnimationUsingKeyFramesEllipseObj, ellipseName); 
       Storyboard.SetTargetProperty(
        colorAnimationUsingKeyFramesEllipseObj, new PropertyPath("(Shape.Fill).(SolidColorBrush.Color)")); 
       colorAnimationUsingKeyFramesEllipseObj.KeyFrames.Add(easingColorKeyFrameEllipseObj); 

       EasingColorKeyFrame easingColorKeyFrameTextBlockObj = new EasingColorKeyFrame(); 
       easingColorKeyFrameTextBlockObj.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(12)); 
       easingColorKeyFrameTextBlockObj.Value = textBlockColor; 

       ColorAnimationUsingKeyFrames colorAnimationUsingKeyFramesTextBlockObj = new ColorAnimationUsingKeyFrames(); 
       Storyboard.SetTargetName(colorAnimationUsingKeyFramesTextBlockObj, textBlockName); 
       Storyboard.SetTargetProperty(
        colorAnimationUsingKeyFramesTextBlockObj, new PropertyPath("(TextElement.Background).(SolidColorBrush.Color)")); 
       colorAnimationUsingKeyFramesTextBlockObj.KeyFrames.Add(easingColorKeyFrameTextBlockObj); 


       Storyboard storyboard = new Storyboard(); 

       storyboard.Children.Add(colorAnimationUsingKeyFramesEllipseObj); 
       storyboard.Children.Add(colorAnimationUsingKeyFramesTextBlockObj); 

       storyboard.Begin(this, true); 
      }