我尝试使用相对绑定,并将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。加载的动画不工作,但如果我将路由事件更改为MouseEnter动画工作。我发布了所有项目。
示例下载
感谢
奇怪的是,如果有frameworkElement traverses Visual Tree till ancestral window
,在这种情况下是DoubleAnimation too is able to find ancestral window
,但如果没有元素引用祖先窗口,DoubleAnimation无法遍历Visual树。
在你的例子中,如果我只是在动画之外遍历直到窗口,它就可以工作了。测试此样品(traverse tree using Tag property of canvas
)-
<Canvas Height="400"
Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=Window}}"> <-HERE
<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>