WPF Animate 边框背景颜色

WPF Animate border background color

我正在尝试为继承自 Border 的自定义 class 的背景设置画笔颜色动画。我在这里尝试了 MSDN link:

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

这不是我要找的东西,但可以让我毫无错误地到达某个点,但仍然没有任何动画。该示例的问题在于它们在 class 中定义了不是矩形的逻辑。我正在尝试从矩形内(实际上是边框)进行定义。

下面是我尝试从 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 个解决方案,但我希望这个 class 将来可以重用,重新定义这个逻辑并不理想。

而不是 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 的 constructor 中设置 Background 如下:

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