我正在用C#开发wpf应用程序。以下xaml代码对我来说很好。
<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);
在上面的代码中,如何在C#中设置Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
和Storyboard.TargetProperty="(TextElement.Background).(SolidColorBrush.Color)"
答案 0 :(得分: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);
}