【问题标题】:WPF using VisualStateManager to animate panels in & outWPF 使用 VisualStateManager 为面板输入和输出动画
【发布时间】:2011-02-22 21:30:12
【问题描述】:

我希望我正在尝试做的事情是可以使用视觉状态...

我想要做的是有一个按钮,可以在两种状态之间切换 stackPanel:“In”和“Out”。然后我会在按钮的单击事件上调用 VisualStateManager.GoToState,以在两种状态之间切换。

我遇到的问题是我不知道如何将状态附加到 StackPanel。它不会让我使用 Expression Blend。所以我被困住了......无论如何,是否可以使用 VisualStateManager 为这个面板设置动画? (我知道我可以使用 Storyboard 并创建进出动画,但如果可能的话,我更喜欢使用 State)

我真的希望这是可能的。否则 VisualStateManager 除了在按钮上做花哨的翻转效果还有什么好处?

感谢您的帮助!

【问题讨论】:

  • 我也尝试将 VisualStateGroup 添加到窗口的 LayoutRoot,然后在 Click 事件中将“this”传递给 VSM(例如:VisualStateManager.GoToState(this,"In", true )...但这对我也不起作用。
  • 奇怪...我只是将相同的代码放入 WinPhone7 项目中,它运行良好...为什么它适用于 WinPhone7 Silverlight,而不适用于 WPF?

标签: wpf silverlight expression-blend visualstatemanager


【解决方案1】:

刚刚启动 Blend 并得到了这个:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    x:Class="WpfApplication1.MainWindow"
    x:Name="Window"
    Title="MainWindow"
    Width="640" Height="480">

    <StackPanel x:Name="LayoutRoot">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="PanelState">
                <VisualState x:Name="In"/>
                <VisualState x:Name="Out">
                    <Storyboard>
                        <ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="stackPanel">
                            <EasingThicknessKeyFrame KeyTime="0" Value="-65,15,0,0"/>
                        </ThicknessAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <i:Interaction.Behaviors>
            <ei:DataStateBehavior Binding="{Binding IsChecked, ElementName=toggleButton}" Value="True" TrueState="In" FalseState="Out"/>
        </i:Interaction.Behaviors>
        <Button Content="Button" Width="50" HorizontalAlignment="Left" Click="Button_Click"/>
        <StackPanel x:Name="stackPanel" Height="100" HorizontalAlignment="Left" Margin="0,15,0,0">
            <TextBlock><Run Text="Funnytext"/></TextBlock>
        </StackPanel>
        <ToggleButton x:Name="toggleButton" Content="Toggle" Width="50" HorizontalAlignment="Left"/>
    </StackPanel>
</Window>

和后面的代码:

private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
    var sgs=VisualStateManager.GetVisualStateGroups(LayoutRoot);
    var sg=sgs[0] as VisualStateGroup;
    VisualStateManager.GoToElementState(LayoutRoot, ((VisualState) sg.States[sg.CurrentState == sg.States[0]?1:0]).Name,true);
}

(不知道你的意思是什么stackpanel,所以我只包括了两个)

编辑:我的错,没有注意到我没有包含点击处理程序。为了您的方便,我提供了一个使用 Togglebutton 来切换状态的示例......

【讨论】:

  • 您甚至没有向按钮添加事件处理程序...您想向我展示什么?我的问题是,使用您拥有的这段代码,您将如何将状态从 out 更改为 in?您在代码隐藏中将哪些参数传递给 VSM?
  • 太好了。谢谢!因此,与 GoToState 相比,WPF 似乎最容易使用 GoToElementState,因为视觉状态应用于 LayoutRoot。 (抱歉耽搁了,周末没能赶到)
猜你喜欢
  • 1970-01-01
  • 2012-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-24
  • 2014-10-08
  • 1970-01-01
相关资源
最近更新 更多