0

我正在尝试为继承自的自定义类的背景设置画笔颜色动画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 个解决方案,但我希望这个类在未来可以重用,重新定义这个逻辑并不理想。

4

1 回答 1

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 Backgroundconstructor

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);
    }
}
于 2015-01-08T03:57:07.683 回答