0

我尝试使用相对绑定并将 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 动画工作。我发布了所有项目。

样本下载

感谢

4

1 回答 1

1

奇怪的是,如果有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>
于 2013-12-05T21:00:51.617 回答