【问题标题】:WPF DoubleAnimation from CodeBehind来自 CodeBehind 的 WPF DoubleAnimation
【发布时间】:2014-05-09 13:40:54
【问题描述】:

在 MainWindow.xaml 我有这个:

<view:CircularProgressBar Percentage="25"
                          ...
                          x:Name="speedoBar"/>

我将百分比绑定到我从更远的外部获得的值。问题是:它直接设置值,但我需要一个 DoubleAnimation 从我现在的位置到我刚刚得到的值。

在获得价值的部分中,我尝试创建一个新的 Storyboard 和 DoubleAnimation,但我没有尝试任何工作。 我可以考虑创建一个新的 DependencyProperty 变量,该变量将是 DoubleAnimated 并将 Percentage 绑定到它。但是这个值是双精度值,我无法将 DoubleAnimation 启动为通常的双精度值。我在互联网上只能找到对象 DpendencyProperty 的 DoubleAnimation。

然后我尝试在百分比值上制作动画,但代码隐藏无法识别它,VisualStudio 建议创建一个新的 DependencyProperty-variable。

到目前为止,我所拥有的是:

// get the old value
speedCopy = speedoBar.Percentage;

DoubleAnimation speedDoubleAni =
new DoubleAnimation(speedCopy, (double)(vehicleDataViewModel.Speed), new Duration(new TimeSpan(0, 0, 10)));

Storyboard.SetTargetName(speedDoubleAni, "speedoBar.Percentage");
Storyboard.SetTargetProperty(speedDoubleAni, new PropertyPath(DoubleAnimation.ByProperty));

Storyboard story = new Storyboard();
story.Children.Add(speedDoubleAni);
story.Begin();

但它不起作用并在story.Begin(); 处显示错误,这意味着更难找出真正的问题 D:

那么你会怎么做呢?我在这里做错了什么?

【问题讨论】:

    标签: c# wpf wpf-animation doubleanimation


    【解决方案1】:

    您将 TargetNameTargetProperty 设置为错误的值。来自MSDN

    • TargetName:获取或设置对象的名称以进行动画处理。该对象必须是 FrameworkElement、FrameworkContentElement 或 Freezable。
    • TargetProperty:获取或设置应该动画的属性

    因此,在您的情况下,TargetName 将是 speedoBarTargetProperty 将是 Percentage,但您可以将 Target 直接设置为 speedoBar,并且假设 CircularProgressBar.PercentageDependencyProperty,如下所示代码应该可以工作:

    Storyboard.SetTarget(speedDoubleAni, speedoBar);
    Storyboard.SetTargetProperty(speedDoubleAni, new PropertyPath("Percentage"));
    

    【讨论】:

    • 在我编辑我的帖子之前,我刚刚刷新以寻找答案。我有第一行,不知何故我没有尝试简单地输入“百分比”。仍然:谢谢。我现在感觉很笨,但是你帮了我很多! :)
    猜你喜欢
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多