更改视觉状态并根据依赖属性使用或不使用转换

发布于 2024-11-02 01:10:34 字数 1823 浏览 0 评论 0原文

我有一个控件,有时我想要动画状态转换,有时却不想。

目前我有这样的东西:

<Grid>
    <Interactivity:Interaction.Behaviors>
        <ic:DataStateBehavior Binding="{Binding Direction}" Value="Up" TrueState="Up_Direction" />
    </Interactivity:Interaction.Behaviors>    
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="AnimatedStates">
            <VisualStateGroup.Transitions>
                <VisualTransition x:Name="transition" GeneratedDuration="0:0:1">
                    <VisualTransition.GeneratedEasingFunction>
                        <ElasticEase .../>
                    </VisualTransition.GeneratedEasingFunction>
                </VisualTransition>
            </VisualStateGroup.Transitions>

            <VisualState x:Name="Up_Direction" >
                <Storyboard>
                    <DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)" 
                        Storyboard.TargetName="pathArrow" />
                </Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <Path x:Name="pathArrow" ...>
        <Path.RenderTransform>
            <RotateTransform Angle="90" />
        </Path.RenderTransform>
    </Path>
</Grid>

这很好用,但我想要一个 UseTransitions 依赖属性来控制状态转换是否是动画的。

我尝试了一些方法,例如在 UseTransitions 属性更改处理程序上将 GenerationDuration 设置为 0,但它似乎是在状态已经更改后设置的,因此没有效果。

我还尝试了带有 DataTrigger 的 GoToStateAction,但它们似乎仅在更改时触发,并且 DataContext 的初始值未正确设置状态。

我考虑过在 DataStateBehaviour 上使用 ValueConverter 来评估 UseTransition 属性并触发不同的状态,但这似乎是一个非常丑陋的解决方案。

有人有一个优雅的解决方案吗?

I have one control, that I sometimes want to animate a state transition and sometimes I don't.

At the moment I have something like so:

<Grid>
    <Interactivity:Interaction.Behaviors>
        <ic:DataStateBehavior Binding="{Binding Direction}" Value="Up" TrueState="Up_Direction" />
    </Interactivity:Interaction.Behaviors>    
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="AnimatedStates">
            <VisualStateGroup.Transitions>
                <VisualTransition x:Name="transition" GeneratedDuration="0:0:1">
                    <VisualTransition.GeneratedEasingFunction>
                        <ElasticEase .../>
                    </VisualTransition.GeneratedEasingFunction>
                </VisualTransition>
            </VisualStateGroup.Transitions>

            <VisualState x:Name="Up_Direction" >
                <Storyboard>
                    <DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)" 
                        Storyboard.TargetName="pathArrow" />
                </Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <Path x:Name="pathArrow" ...>
        <Path.RenderTransform>
            <RotateTransform Angle="90" />
        </Path.RenderTransform>
    </Path>
</Grid>

This works great, but I want to have a UseTransitions dependency property that controls whether that state transitions are animated or not.

I've tried a few things like setting the GeneratedDuration to 0 on the UseTransitions property changed handler, but it seems to get set after the state has already changed, so has no effect.

I also tried the GoToStateAction with a DataTrigger but they only seem to get triggered on a change and the initial value of the DataContext doesn't set the state correctly.

I've thought about having a ValueConverter on the DataStateBehaviour that evaluates the UseTransition property and triggers different states, but that seems like a really ugly solution.

Anyone have an elegant solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文