首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自DoubleAnimation的WPF CodeBehind

来自DoubleAnimation的WPF CodeBehind
EN

Stack Overflow用户
提问于 2014-05-09 13:40:54
回答 1查看 2.6K关注 0票数 1

在MainWindow.xaml中,我有以下内容:

代码语言:javascript
复制
<view:CircularProgressBar Percentage="25"
                          ...
                          x:Name="speedoBar"/>

我的百分比与我从更远的地方得到的值相联系。问题是:它直接设置值,但我需要有一个DoubleAnimation,从我现在的位置到我刚刚得到的值。

在我获得价值的部分,我试图创建一个新的故事板和DoubleAnimation,但没有什么效果。我可以考虑创建一个新的DependencyProperty变量,它将是DoubleAnimated并绑定到它。但是这个值将是一个双值,并且我无法启动一个DoubleAnimation到一个通常的双值。我在互联网上只能找到一个DoubleAnimation到一个对象的DpendencyProperty。

然后,我尝试在百分比值上做动画,但是代码背后没有识别它,VisualStudio建议创建一个新的DependencyProperty变量。

到目前为止,我得到的是:

代码语言:javascript
复制
// 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:

你怎么做到这一点,我在这里做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-09 13:59:10

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

  • TargetName:获取或设置要动画的对象的名称。对象必须是FrameworkElement、FrameworkContentElement或可冻结的。
  • TargetProperty:获取或设置应该是动画的属性。

因此,在您的示例中,TargetNamespeedoBarTargetPropertyPercentage,但是您可以直接将Target设置为speedoBar,并且假设CircularProgressBar.Percentage是一个DependencyProperty,下面的代码应该可以工作:

代码语言:javascript
复制
Storyboard.SetTarget(speedDoubleAni, speedoBar);
Storyboard.SetTargetProperty(speedDoubleAni, new PropertyPath("Percentage"));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23566141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档