VisualStateManager中是否支持Databinding或TemplateBinding

本文关键字:TemplateBinding Databinding 支持 是否 VisualStateManager | 更新日期: 2023-09-27 18:27:19

我正试图基于绑定值延迟自定义控件的动画。在下面的示例中,我希望动画在选择"SelectedAndHit"视觉状态后5秒开始。但是,在VisualStateManage中似乎不可能使用模板绑定。

VisualStateManager中是否支持TemplateBinding?有什么变通办法吗?

<local:ButtonEx x:Name="Button01" AnimationBeginTime="00:00:05" />
public TimeSpan AnimationBeginTime
{
    get { return (TimeSpan)base.GetValue(ButtonEx.AnimationBeginTimeProperty); }
    set { base.SetValue(ButtonEx.AnimationBeginTimeProperty, value); }
}
public static readonly DependencyProperty AnimationBeginTimeProperty =
   DependencyProperty.Register("AnimationBeginTime", typeof(TimeSpan), typeof(ButtonEx), new PropertyMetadata(TimeSpan.Zero));
<Style TargetType="local:ButtonEx">
    <!-- ... -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:ButtonEx">
                <Grid x:Name="Container" RenderTransformOrigin="0.5, 0.5">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal" />
                            <VisualState x:Name="SelectedAndHit">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border" 
                                                                   Storyboard.TargetProperty="Background">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBackgroundColorSelectedAndHit}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <Storyboard>
                                        <DoubleAnimation
                                            Storyboard.TargetName="GridScaleTransform"
                                            Storyboard.TargetProperty="(ScaleTransform.ScaleX)"
                                            To="1.2" BeginTime="{TemplateBinding AnimationBeginTime}" Duration="00:00:00.300" AutoReverse="True">
                                            <DoubleAnimation.EasingFunction>
                                                <ExponentialEase EasingMode="EaseIn" />
                                            </DoubleAnimation.EasingFunction>
                                        </DoubleAnimation>
                                        <DoubleAnimation
                                            Storyboard.TargetName="GridScaleTransform"
                                            Storyboard.TargetProperty="(ScaleTransform.ScaleY)"
                                            To="1.2" BeginTime="{TemplateBinding AnimationBeginTime}" Duration="00:00:00.300" AutoReverse="True">
                                            <DoubleAnimation.EasingFunction>
                                                <ExponentialEase EasingMode="EaseIn" />
                                            </DoubleAnimation.EasingFunction>
                                       </DoubleAnimation>
                                    </Storyboard>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Grid.RenderTransform>
                        <ScaleTransform x:Name="GridScaleTransform" />
                    </Grid.RenderTransform>
                    <!-- ... -->
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

VisualStateManager中是否支持Databinding或TemplateBinding

我确认WinRT不支持样式中的Binding/TemplateBinding。为了解决这个问题,我编写了手动更新情节提要的BeginTime的代码。这样一来,我就可以完全控制故事板何时开始。

我会看看交互性。我个人在GoToStateAction中使用了EventTrigger,这对我来说已经足够了。从MSDN来看,您可能能够使用TimerTrigger和GoToStateAction来创建您想要的效果。TimerTrigger具有用于将操作的延迟设置为激发的依赖属性。