WPF 使用 VisualStateManager 在 & 中对面板进行动画处理出去

发布于 2024-10-18 20:55:37 字数 444 浏览 4 评论 0原文

我希望我想做的事情可以使用视觉状态来实现...

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

Panel States

我遇到的问题是我不知道如何将状态附加到 StackPanel。它不允许我使用 Expression Blend。所以我被困住了......有没有办法使用 VisualStateManager 动画进出这个面板? (我知道我可以使用故事板并创建进出动画,但如果可能的话我更愿意使用状态)

我真的希望这是可能的。否则,除了对按钮进行花哨的翻转效果之外,VisualStateManager 还有什么用呢?

感谢您的帮助!

I'm hoping what I'm trying to do is possible using Visual States...

What I want to do is have a button that toggles a stackPanel between two states: 'In' and 'Out'. I would then call VisualStateManager.GoToState on the button's click event, to toggle between the two states.

Panel States

The problem I've run into is I can't figure out how to attach states to the StackPanel. It won't let me using Expression Blend. So I'm stuck... Is there anyway to animate in and out this panel using VisualStateManager? (I know I could use Storyboards and create and In and Out animation, but I'd preffer to use States if possible)

I really hope this is possible. Otherwise what is VisualStateManager good for besides doing gimmicky rollover effects on buttons?

Thanks for any help!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷遇一生 2024-10-25 20:55:37

刚刚启动 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);
}

不知道你的意思是堆栈面板,所以我只包含两个)

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

just fired up Blend and got this:

<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>

and code behind:

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);
}

(didn't know what stackpanel you meant so i just included two)

EDIT: my bad, didn't notice I didn't include the clickhandler. For for your convenience I included an example to use a Togglebutton to toggle the states...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文