【问题标题】:WPF Animate border background colorWPF 动画边框背景颜色
【发布时间】:2015-01-08 02:20:51
【问题描述】:

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

【问题讨论】:

    标签: wpf background border wpf-animation coloranimation


    【解决方案1】:

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

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

    【讨论】:

    • 这会在 mouseEnter 上引发 InvalidOperationExceptionBorderBrush 不指向 DeependancyObjectin 路径 (Border.BorderBrush).(SolidColorBrush.Color)
    • this.BorderBrush = new SolidColorBrush(origColor.Color);将此行放入构造函数中...
    • 仍然没有动画。感谢所有的帮助,顺便说一句。
    • @JustinGrahn 但它绝对可以在我的机器上运行.. 你能发布你在哪里使用这个PrettyButton 吗??
    • 我已经在示例应用程序中进行了测试。我刚刚在一个空白的白色背景上添加了一个测试PrettyButton,看看是否有什么我没有注意到的变化。您的解决方案确实更改了边框颜色,但没有更改背景颜色,这是我需要更改的。我希望边框不可见。它会为你改变背景颜色吗?
    猜你喜欢
    • 2012-12-18
    • 2014-05-14
    • 1970-01-01
    • 2015-07-05
    • 2018-10-13
    • 2023-03-11
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多