【问题标题】:Use relative binding to bind prop Window.ActualHeight to DoubleAnimation.To prop使用相对绑定将道具 Window.ActualHeight 绑定到 DoubleAnimation.To 道具
【发布时间】: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 动画工作。我发布了所有项目。

sample download

谢谢

【问题讨论】:

  • 我猜测视觉树没有将&lt;DoubleAnimation&gt; 放在&lt;Window&gt; 下,所以RelativeSource 绑定从&lt;DoubleAnimation&gt; 向上遍历视觉树时找不到它。这只是一个猜测,我还没有测试过:)

标签: wpf animation binding relativesource


【解决方案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>

【讨论】:

  • 抱歉,Height 和 ActualHeight 出现错误。 Window Height 属性设置为 350(查看 XAML)。相对绑定不适用于实际高度,也不适用于高度。
  • 我再次测试了路由事件 Ellipse.Loaded 动画不起作用但如果我将路由事件更改为 MouseEnter 动画工作。我发布了所有项目。你在什么事件上触发事件触发器?
  • 事件无关紧要。无论您绑定什么事件,Visual Tree 都将是相同的。顺便说一句,它仅适用于 Ellipse.Loaded 事件。
  • 所以我们必须有一个不同的代码我不相信我的电脑和你的作品上的相同代码不起作用:)
  • 你试过在新的 WPF 应用程序中测试它吗?或者您可以在绑定中使用转换器,以确保该值被绑定评估。
猜你喜欢
  • 1970-01-01
  • 2018-03-07
  • 1970-01-01
  • 2020-09-19
  • 2022-07-05
  • 2018-08-03
  • 2020-03-10
  • 1970-01-01
  • 2020-01-17
相关资源
最近更新 更多