简体   繁体   中英

How to set Storyboard.TargetProperty in C#?

I am developing wpf application in C#. The following xaml code is working fine for me.

<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#

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#

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);
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM