wpf 如何在 C# 中设置 Storyboard.TargetProperty?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13217094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set Storyboard.TargetProperty in C#?
提问by Shailesh Jaiswal
I am developing wpf application in C#. The following xaml code is working fine for me.
我正在用 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>
The same code I have written in C#
我用 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);
In the above code how should I set the Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"and Storyboard.TargetProperty="(TextElement.Background).(SolidColorBrush.Color)"in C#
在上面的代码中,我应该如何在 C# 中设置Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"andStoryboard.TargetProperty="(TextElement.Background).(SolidColorBrush.Color)"
采纳答案by Shailesh Jaiswal
You need to specify it as a string
您需要将其指定为字符串
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);
}

