1

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

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

面板状态

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

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

谢谢你的帮助!

4

1 回答 1

2

刚刚启动 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 来切换状态的示例......

于 2011-02-22T21:56:11.137 回答