【发布时间】:2013-12-23 00:08:10
【问题描述】:
我尝试使用相对绑定并将 Window.ActualHeight 属性绑定到 DoubleAnimation.To 属性。
相对绑定不起作用,但如果我通过 ElementName 使用绑定,它会起作用。
不工作:
<Window xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Sample.Window"
x:Name="Shell"
Title="Sample"
Width = "525"
Height = "350">
<Canvas Height="400">
<Ellipse Canvas.Left="80"
Canvas.Top="0"
Width="30"
Height="30"
Fill="LimeGreen">
<Ellipse.Triggers>
<EventTrigger RoutedEvent="Ellipse.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Top)"
Duration="0:0:5"
RepeatBehavior="Forever"
AutoReverse="True"
To="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}, Path=ActualHeight}" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
</Canvas>
</Window>
这项工作:
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Top)"
Duration="0:0:5"
RepeatBehavior="Forever"
AutoReverse="True"
To="{Binding ElementName=Shell, Path=ActualHeight}"/>
是否可以使用相对绑定到 Window.ActualHeight 属性?
已编辑:
我再次测试了路由事件 Ellipse.Loaded 动画不起作用,但如果我将路由事件更改为 MouseEnter 动画工作。我发布了所有项目。
谢谢
【问题讨论】:
-
我猜测视觉树没有将
<DoubleAnimation>放在<Window>下,所以RelativeSource绑定从<DoubleAnimation>向上遍历视觉树时找不到它。这只是一个猜测,我还没有测试过:)
标签: wpf animation binding relativesource