WPF动画边框背景颜色

时间:2015-01-08 02:20:52

标签: wpf background border wpf-animation coloranimation

我试图为从Border继承的自定义类的背景设置画笔颜色的动画。我在这里尝试过MSDN链接:

http://msdn.microsoft.com/en-us/library/system.windows.media.animation.coloranimation.aspx

这并不是我正在寻找的,但可以让我达到一个没有错误的地步,但仍然没有任何动画。该示例的问题在于它们在不是矩形的类中定义逻辑。我试图从矩形(实际上是边界)内定义。

以下是我尝试根据我的情况从MSDN推断的代码。

public class PrettyButton : System.Windows.Controls.Border
{
    private System.Windows.Media.SolidColorBrush hoverColor = new System.Windows.Media.SolidColorBrush();
    private System.Windows.Media.SolidColorBrush origColor = new System.Windows.Media.SolidColorBrush();
    private System.Windows.Media.Animation.Storyboard story = new System.Windows.Media.Animation.Storyboard();

    public PrettyButton()
    {
        hoverColor.Color = System.Windows.Media.Color.FromArgb(255, 50, 200, 0);
        origColor.Color = System.Windows.Media.Color.FromArgb(0, 0, 0, 0);

        this.MouseEnter += PrettyButton_MouseEnter;
        this.MouseLeave += PrettyButton_MouseLeave;

        //Animate in logic
        System.Windows.Media.Animation.ColorAnimation color = new System.Windows.Media.Animation.ColorAnimation(hoverColor.Color, System.TimeSpan.FromMilliseconds(400));
        System.Windows.Media.Animation.Storyboard.SetTargetProperty(color, new System.Windows.PropertyPath(System.Windows.Media.SolidColorBrush.ColorProperty));

        story.Children.Add(color);            
    }

以及我在mouseEvent中的

void PrettyButton_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        story.Begin(this);            
    }

不幸的是,我没有再收到任何错误,所以这条路对我来说已经很冷了。我也确信我可以在XAML中找到10个解决方案,但我希望将来可以重用这个类,重新定义这个逻辑并不理想。

1 个答案:

答案 0 :(得分:1)

而不是System.Windows.Media.SolidColorBrush.ColorProperty,请尝试设置"(Border.Background).(SolidColorBrush.Color)" 物业路径。

System.Windows.Media.Animation.Storyboard.SetTargetProperty(color, new System.Windows.PropertyPath("(Border.Background).(SolidColorBrush.Color)"));

同样在PrettyButton的Background中设置constructor,如下所示:

public PrettyButton()
{
    .....
    origColor.Color = System.Windows.Media.Color.FromArgb(0, 0, 0, 0);
    this.Background= new SolidColorBrush(origColor.Color);
    ..
    ....

}

更新:

public class PrettyButton : System.Windows.Controls.Border
{
    private System.Windows.Media.SolidColorBrush hoverColor = new System.Windows.Media.SolidColorBrush();
    private System.Windows.Media.SolidColorBrush origColor = new System.Windows.Media.SolidColorBrush();


    public PrettyButton()
    {
        hoverColor.Color = System.Windows.Media.Color.FromArgb(255, 50, 200, 0);
        origColor.Color = System.Windows.Media.Color.FromArgb(0, 0, 0, 0);

        this.Background= new SolidColorBrush(origColor.Color);
        this.MouseEnter += PrettyButton_MouseEnter;
        this.MouseLeave += PrettyButton_MouseLeave;

    }

    private void PrettyButton_MouseLeave(object sender, MouseEventArgs e)
    {
        System.Windows.Media.Animation.Storyboard story = new System.Windows.Media.Animation.Storyboard();
        System.Windows.Media.Animation.ColorAnimation color = new System.Windows.Media.Animation.ColorAnimation(origColor.Color, System.TimeSpan.FromMilliseconds(400));
        System.Windows.Media.Animation.Storyboard.SetTargetProperty(color, new System.Windows.PropertyPath("(Border.Background).(SolidColorBrush.Color)"));

        story.Children.Add(color);
        story.Begin(this);
    }

    private void PrettyButton_MouseEnter(object sender, MouseEventArgs e)
    {
        System.Windows.Media.Animation.Storyboard story = new System.Windows.Media.Animation.Storyboard();
        System.Windows.Media.Animation.ColorAnimation color = new System.Windows.Media.Animation.ColorAnimation(hoverColor.Color, System.TimeSpan.FromMilliseconds(400));
        System.Windows.Media.Animation.Storyboard.SetTargetProperty(color, new System.Windows.PropertyPath("(Border.Background).(SolidColorBrush.Color)"));

        story.Children.Add(color);
        story.Begin(this);
    }
}