我正在用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)"

最佳答案

您需要将其指定为字符串

关于c# - 如何在C#中设置Storyboard.TargetProperty?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13217094/

10-10 18:35