2

我在 Button.Trigger 中使用了一个简单的 DoubleAnimation。动画 From 绑定到根元素 ActualHeight。这按预期工作。

接下来,我尝试将 Storyboard 移至 VisualStateManager。现在 WPF 抱怨:

System.Windows.Data 错误:2:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement。绑定表达式:路径=实际宽度;数据项=空;目标元素是“DoubleAnimation”(HashCode=29201322);目标属性是'To'(类型'Nullable`1')

不能在 VisualStateManager.VisualStateGroups/VisualState 内的动画中使用绑定吗?

似乎没有。我通过将故事板移动到资源来解决问题。所以现在 GameToTitle 将工作,而 TitleToGame 将失败。

我仍然很高兴知道这是否是预期的。

相关 XAML:

<Grid x:Name="MainInterface">
    <Grid.Resources>
        <Storyboard x:Key="GameToTitle">
            <DoubleAnimation Storyboard.TargetName="GameScreenInterface" Storyboard.TargetProperty="(Control.RenderTransform).(TransformGroup.Children)[1].(TranslateTransform.Y)" From="0" To="{Binding Path=ActualHeight, ElementName=MainInterface}" Duration="0:0:0.2"/>
            <DoubleAnimation Storyboard.TargetName="TitleScreenInterface" Storyboard.TargetProperty="(Control.RenderTransform).(TransformGroup.Children)[1].(TranslateTransform.Y)" From="{Binding Path=ActualHeight, ElementName=MainInterface, Converter={Converters:InverseConverter}}" To="0" Duration="0:0:0.2"/>
        </Storyboard>
    </Grid.Resources>
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup>
            <VisualState x:Name="TitleScreenState" Storyboard="{StaticResource ResourceKey=GameToTitle}"/>
            <VisualState x:Name="GameScreenState">
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="GameScreenInterface" Storyboard.TargetProperty="(Control.RenderTransform).(TransformGroup.Children)[1].(TranslateTransform.Y)" From="{Binding Path=ActualHeight, ElementName=MainInterface}" To="0" Duration="0:0:0.2"/>
                        <DoubleAnimation Storyboard.TargetName="TitleScreenInterface" Storyboard.TargetProperty="(Control.RenderTransform).(TransformGroup.Children)[1].(TranslateTransform.Y)" From="0" To="{Binding Path=ActualHeight, ElementName=MainInterface, Converter={Converters:InverseConverter}}" Duration="0:0:0.2"/>
                </Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <View:TitleScreen x:Name="TitleScreenInterface"/>
    <View:GameScreen x:Name="GameScreenInterface" />
</Grid>
4

1 回答 1

3

正如问题的评论所指出的那样,包含绑定的情节提要必须在 VisualStateGroup 中声明为资源而不是内联才能使绑定起作用。这是因为 VisualStates 不是 VisualTree 的一部分。另一方面,资源是。

您可以通过使用 Snoop(或类似的实用程序)查看应用程序的可视化树,自己将其可视化。您会注意到资源节点出现在 Snoop 的可视化树中,但 VisualStates 没有。

出现在视觉树中的情节提要。

于 2012-12-13T13:34:03.137 回答