0

我已经能够在我的子类 TabItem 中使用以下代码执行上述操作:

protected override void OnSelected(RoutedEventArgs e)
    {
        base.OnSelected(e);

        if (this.StoryBoard == null)
        {
            ColorAnimation anim = new ColorAnimation(Colors.Transparent, Colors.AliceBlue, new Duration(TimeSpan.FromSeconds(1)))
            {
                AutoReverse = true,
                RepeatBehavior = RepeatBehavior.Forever
            };

            Storyboard.SetTarget(anim, this);
            Storyboard.SetTargetProperty(anim, new PropertyPath("Background.Color"));

            Storyboard sb = new Storyboard();
            sb.Children.Add(anim);

            this.StoryBoard = sb;
        }

        VisualTree.FindParent<OMWTabControl>(this).Items.Cast<OMWTabItem>().ToList().ForEach(n =>
        {
            if (n.StoryBoard != null)
            {
                n.StoryBoard.Stop();
            }
        });

        this.StoryBoard.Begin();
    }

我知道我正在采用正确的方法来做到这一点 - 我应该使用我相信的 DataTriggers 和 Setters。

我已经进行了广泛的搜索,所有内容都在 XAML 中,大部分都可以解释为 C#,但不是全部。

有人可以指出我在后面的代码中执行此操作的“正确”方法吗?

4

1 回答 1

0

每当您想了解任何方法、接口、类或任何有关 .NET Framework 的信息时,只需访问 MSDN。您可以访问 MSDN 上的Storyboard.TargetProperty 附加属性页面,以获得对您问题的快速回答。从链接页面:

public StoryboardExample()
{
    // Create a name scope for the page.
    NameScope.SetNameScope(this, new NameScope());

    this.WindowTitle = "Animate Properties using Storyboards";
    StackPanel myStackPanel = new StackPanel();
    myStackPanel.MinWidth = 500;
    myStackPanel.Margin = new Thickness(30);
    myStackPanel.HorizontalAlignment = HorizontalAlignment.Left;
    TextBlock myTextBlock = new TextBlock();
    myTextBlock.Text = "Storyboard Animation Example";
    myStackPanel.Children.Add(myTextBlock);

    // 
    // Create and animate the first button. 
    // 

    // Create a button.
    Button myWidthAnimatedButton = new Button();
    myWidthAnimatedButton.Height = 30;
    myWidthAnimatedButton.Width = 200;
    myWidthAnimatedButton.HorizontalAlignment = HorizontalAlignment.Left;
    myWidthAnimatedButton.Content = "A Button";

    // Set the Name of the button so that it can be referred 
    // to in the storyboard that's created later. 
    // The ID doesn't have to match the variable name; 
    // it can be any unique identifier.
    myWidthAnimatedButton.Name = "myWidthAnimatedButton";

    // Register the name with the page to which the button belongs. 
    this.RegisterName(myWidthAnimatedButton.Name, myWidthAnimatedButton);

    // Create a DoubleAnimation to animate the width of the button.
    DoubleAnimation myDoubleAnimation = new DoubleAnimation();
    myDoubleAnimation.From = 200;
    myDoubleAnimation.To = 300;
    myDoubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(3000));          

    // Configure the animation to target the button's Width property.
    Storyboard.SetTargetName(myDoubleAnimation, myWidthAnimatedButton.Name); 
    Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Button.WidthProperty));

    // Create a storyboard to contain the animation.
    Storyboard myWidthAnimatedButtonStoryboard = new Storyboard();
    myWidthAnimatedButtonStoryboard.Children.Add(myDoubleAnimation);

    // Animate the button width when it's clicked.
    myWidthAnimatedButton.Click += delegate(object sender, RoutedEventArgs args)
        {
            myWidthAnimatedButtonStoryboard.Begin(myWidthAnimatedButton);
        };


    myStackPanel.Children.Add(myWidthAnimatedButton);

    // 
    // Create and animate the second button. 
    // 

    // Create a second button.
    Button myColorAnimatedButton = new Button();
    myColorAnimatedButton.Height = 30;
    myColorAnimatedButton.Width = 200;
    myColorAnimatedButton.HorizontalAlignment = HorizontalAlignment.Left;
    myColorAnimatedButton.Content = "Another Button";

    // Create a SolidColorBrush to paint the button's background.
    SolidColorBrush myBackgroundBrush = new SolidColorBrush();
    myBackgroundBrush.Color = Colors.Blue;

    // Because a Brush isn't a FrameworkElement, it doesn't 
    // have a Name property to set. Instead, you just 
    // register a name for the SolidColorBrush with 
    // the page where it's used. 
    this.RegisterName("myAnimatedBrush", myBackgroundBrush);

    // Use the brush to paint the background of the button.
    myColorAnimatedButton.Background = myBackgroundBrush;

    // Create a ColorAnimation to animate the button's background.
    ColorAnimation myColorAnimation = new ColorAnimation();
    myColorAnimation.From = Colors.Red;
    myColorAnimation.To = Colors.Blue;
    myColorAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(7000));    

    // Configure the animation to target the brush's Color property.
    Storyboard.SetTargetName(myColorAnimation, "myAnimatedBrush");                        
    Storyboard.SetTargetProperty(myColorAnimation, new PropertyPath(SolidColorBrush.ColorProperty));    

    // Create a storyboard to contain the animation.
    Storyboard myColorAnimatedButtonStoryboard = new Storyboard();
    myColorAnimatedButtonStoryboard.Children.Add(myColorAnimation);

    // Animate the button background color when it's clicked.
    myColorAnimatedButton.Click += delegate(object sender, RoutedEventArgs args)
        {
            myColorAnimatedButtonStoryboard.Begin(myColorAnimatedButton);
        };


    myStackPanel.Children.Add(myColorAnimatedButton);
    this.Content = myStackPanel;

}

请访问链接页面以获取更多信息。

于 2014-06-06T17:24:02.620 回答