Wpf 使用相对绑定将prop Window.ActualHeight绑定到DoubleAnimation.to prop
我尝试使用相对绑定并将Window.ActualHeight属性绑定到DoubleAnimation.to属性 相对绑定不起作用,但如果我按ElementName使用绑定,它就会起作用 不工作:Wpf 使用相对绑定将prop Window.ActualHeight绑定到DoubleAnimation.to prop,wpf,animation,binding,relativesource,Wpf,Animation,Binding,Relativesource,我尝试使用相对绑定并将Window.ActualHeight属性绑定到DoubleAnimation.to属性 相对绑定不起作用,但如果我按ElementName使用绑定,它就会起作用 不工作: <Window xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
<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 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属性的相对绑定
编辑:
<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}"/>
我在路由事件椭圆上再次测试。加载的动画不工作,但如果我将路由事件更改为MouseEnter动画工作。我发布了所有的项目
奇怪的是,如果任何
框架元素遍历视觉树直到祖先窗口,在这种情况下DoubleAnimation也能找到祖先窗口
,但是如果没有元素引用祖先窗口,DoubleAnimation就不能遍历视觉树
在您的示例中,如果我只是遍历动画外的窗口,它就会工作。测试此示例(使用canvas的标记属性遍历树
)-
很抱歉,高度和实际高度有误。窗口高度属性设置为350(查看XAML)。相对绑定对实际高度和高度不起作用。我在路由事件椭圆上再次测试。加载的动画不起作用,但如果我将路由事件更改为MouseEnter动画工作。我发布了所有的项目。你在什么事件上触发事件触发器?事件无关紧要。无论绑定到什么事件,可视化树都是相同的。顺便说一句,它对我的Ellipse有效。只加载了事件。所以我们必须有一个不同的代码。我不相信在我的电脑和你的作品上相同的代码不起作用:)你试过在新鲜的WPF应用程序中测试它吗?或者,您可以在绑定中使用转换器,以确保值是绑定计算到的。我猜可视化树不会将
放在
下,因此相对资源
绑定在从
向上走到可视化树时找不到它。不过这只是一个猜测,我还没有测试过:)