简体   繁体   English

隐藏WPF StackPanel(非扩展器)时出现问题

[英]Problems hiding WPF StackPanel (not expander)

I have a real trouble with WPF StackPanel - I need to make it animated not only for scrolling its content horizontaly (this is ok more or less), but also I must make it expand/collapse (animated) by pressing some button on the containing window. 我在WPF StackPanel遇到了一个真正的麻烦-我不仅需要使其水平移动以使其具有动画效果(这或多或少都是可以的),而且还必须通过按以下内容使它展开/折叠(动画化),窗口。

I have tried a lot of animated expander controls (for example http://www.codeproject.com/KB/WPF/AnimatingExpander.aspx ) but they have overloaded with functionality (and some artifacts with contained controls) and not suitable for my task. 我已经尝试了很多动画扩展器控件(例如http://www.codeproject.com/KB/WPF/AnimatingExpander.aspx ),但它们的功能(以及包含控件的某些工件)已超载,不适合我的任务。

So the question is how to make SIMPLE horizontal oriented StackPanel to expand/collapse with animation by button click? 因此,问题是如何通过单击按钮使SIMPLE水平方向的StackPanel展开/折叠动画?

The simplest approach is to start an animation on a ToggleButton.Checked or .Unchecked event: 最简单的方法是在ToggleButton.Checked.Unchecked事件上启动动画:

<StackPanel x:Name="MyStackPanel">...</StackPanel>

...

<ToggleButton Content="Click Me">
    <ToggleButton.Triggers>
        <EventTrigger RoutedEvent="ToggleButton.Checked">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="MyStackPanel" 
                                     Storyboard.TargetProperty="Width"
                                     To="0"
                                     Duration="0:0:0.5" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="ToggleButton.Unchecked">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="MyStackPanel" 
                                     Storyboard.TargetProperty="Width"
                                     To="200"
                                     Duration="0:0:0.5" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </ToggleButton.Triggers>
</ToggleButton>

Why not adding a storyboard and double animation for stackpanel's width. 为什么不为堆栈面板的宽度添加一个情节提要和双重动画呢? By clicking the button you can start the animation in code or by defining event triggers. 通过单击按钮,您可以通过代码或定义事件触发器来启动动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM