代码中的数据触发器
我已经尝试根据我收到的[使用 WPF 将某些 XAML 转换为 C#][1] 的答案来实现此目的,但我遗漏了一些内容。
我在下面粘贴了我的代码示例。
// uGrid = UniformGrid
uGrid.Columns=2;
// test - setup first ellipse
Ellipse ellipse1 = new Ellipse() { Width=25, Height=25, Fill=Brushes.DodgerBlue, Margin= new Thickness(3), Opacity=0 };
uGrid.Children.Add(ellipse1);
var fadeEllipse1 = new DoubleAnimation() { From = 0, To = 1, Duration = TimeSpan.FromSeconds(this.secondsToFade), FillBehavior=FillBehavior.Stop };
Storyboard.SetTarget(fadeEllipse1, ellipse1);
Storyboard.SetTargetProperty(fadeEllipse1, new PropertyPath(Ellipse.OpacityProperty));
Storyboard storyBoard = new Storyboard();
storyBoard.Children.Add(fadeEllipse1);
storyBoard.RepeatBehavior=RepeatBehavior.Forever;
// test - setup 2nd ellipse using example below
Ellipse ellipse2 = new Ellipse() { Width=25, Height=25, Fill=Brushes.DodgerBlue, Margin= new Thickness(3), Opacity=0 };
uGrid.Children.Add(ellipse2);
var style = new Style(typeof(Ellipse));
var trigger = new DataTrigger();
trigger.Binding = new Binding("Opacity") { ElementName = "ellipse1" };
trigger.Value = 1;
Storyboard sb = new Storyboard();
var fadeEllipse2 = new DoubleAnimation() { From = 0, To = 1, Duration = TimeSpan.FromSeconds(this.secondsToFade), FillBehavior=FillBehavior.Stop };
Storyboard.SetTarget(fadeEllipse2, ellipse2);
Storyboard.SetTargetProperty(fadeEllipse2, new PropertyPath(Ellipse.OpacityProperty));
Storyboard.SetTargetName(fadeEllipse2, ellipse2.Name);
sb.Children.Add(fadeEllipse2);
sb.RepeatBehavior=RepeatBehavior.Forever;
trigger.EnterActions.Add(new BeginStoryboard() { Storyboard = sb });
style.Triggers.Add(trigger);
storyBoard.Begin();
I've tried to get this working, based on the answer I received to [Convert some XAML to C# using WPF][1], but I'm missing something.
I've pasted a sample of my code below.
// uGrid = UniformGrid
uGrid.Columns=2;
// test - setup first ellipse
Ellipse ellipse1 = new Ellipse() { Width=25, Height=25, Fill=Brushes.DodgerBlue, Margin= new Thickness(3), Opacity=0 };
uGrid.Children.Add(ellipse1);
var fadeEllipse1 = new DoubleAnimation() { From = 0, To = 1, Duration = TimeSpan.FromSeconds(this.secondsToFade), FillBehavior=FillBehavior.Stop };
Storyboard.SetTarget(fadeEllipse1, ellipse1);
Storyboard.SetTargetProperty(fadeEllipse1, new PropertyPath(Ellipse.OpacityProperty));
Storyboard storyBoard = new Storyboard();
storyBoard.Children.Add(fadeEllipse1);
storyBoard.RepeatBehavior=RepeatBehavior.Forever;
// test - setup 2nd ellipse using example below
Ellipse ellipse2 = new Ellipse() { Width=25, Height=25, Fill=Brushes.DodgerBlue, Margin= new Thickness(3), Opacity=0 };
uGrid.Children.Add(ellipse2);
var style = new Style(typeof(Ellipse));
var trigger = new DataTrigger();
trigger.Binding = new Binding("Opacity") { ElementName = "ellipse1" };
trigger.Value = 1;
Storyboard sb = new Storyboard();
var fadeEllipse2 = new DoubleAnimation() { From = 0, To = 1, Duration = TimeSpan.FromSeconds(this.secondsToFade), FillBehavior=FillBehavior.Stop };
Storyboard.SetTarget(fadeEllipse2, ellipse2);
Storyboard.SetTargetProperty(fadeEllipse2, new PropertyPath(Ellipse.OpacityProperty));
Storyboard.SetTargetName(fadeEllipse2, ellipse2.Name);
sb.Children.Add(fadeEllipse2);
sb.RepeatBehavior=RepeatBehavior.Forever;
trigger.EnterActions.Add(new BeginStoryboard() { Storyboard = sb });
style.Triggers.Add(trigger);
storyBoard.Begin();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码中有几个错误:
您正在创建一种样式,但您没有在任何地方使用它,您错过了该行
您正在尝试绑定到名为
ellipse1< 的元素/code>,但它不存在(具有该名称的变量不是一回事)。但即使你设置了
ellipse1
的Name
,它也不起作用。您必须在 XAML 中声明该名称。在代码隐藏中,您可以使用Source
并直接绑定到另一个椭圆,而不使用名称(无论如何,这将是一个更好的解决方案,即使使用名称有效):您正在比较两个双精度数是否具有完全相同的值。这几乎总是一个坏主意,尤其是在这种情况下。
ellispse1
的Opacity
不是连续变化的,而是跳跃变化的。默认情况下,该值每秒最多更改 60 次(至少我测试时是这样,我没有在任何地方找到该值的记录)。因此,不透明度
可能恰好从 0 开始,保持该状态 1/60 秒,然后跳至 1/60。因此,比较精确值甚至比通常比较双精度数是否相等(一开始就不可靠)更不可靠。使用转换器检查不透明度是否大于 0.99 似乎对我有用,但它仍然不可靠:如果调度程序很忙,不透明度可能会从 0.85 直接跳到 0.12 .显然
<块引用>
Style 中的 Storyboard 树无法指定 TargetName。删除目标名称。
所以删除下面的就好了,反正也没啥用。
There are several mistakes in your code:
You're creating a style, but you don't use it anywhere, you're missing the line
You're trying to bind to an element with the name
ellipse1
, but it does not exist (having variable with that name is not the same thing). But even if you set theName
ofellipse1
, it wouldn't work. You have to declare the name in XAML for that. In code-behind, you can useSource
and bind to the other ellipse directly, without using names (which would be a better solution anyway, even if using names worked):You're comparing whether two doubles have exactly the same value. That's almost always a bad idea, and in this case especially. The
Opacity
ofellispse1
doesn't change continuously, it changes by jumping. By default, the value changes at most 60 times per second (at least it was that way when I tested it, I didn't find this value documented anywhere). So, theOpacity
might start at exactly 0, stay that way for 1/60 s and then jump to 1/60. Because of this, comparing for exact value is even less reliable than when comparing doubles for equality normally (which is not reliable to start with). Using a converter that checks, whether the opacity is greater than0.99
seems to work for me, but it's still not reliable: if the dispatcher is busy, the opacity can jump, say, from 0.85 straight to 0.12.Apparently
So delete the following like, it's not useful anyway.