【问题标题】:wpf animation to hide and collapse panel on button clickwpf 动画在按钮单击时隐藏和折叠面板
【发布时间】:2017-12-05 12:33:24
【问题描述】:

我有一个 StackPanel 和一个按钮。 Initallty 按钮内容应为 > 正确发生折叠的位置。再次按下同一个按钮时,我希望显示内容并且文本应该是

<Grid>
    <Grid.ColumnDefinitions>
         <ColumnDefinition Width="*"></ColumnDefinition>
         <ColumnDefinition Width="Auto"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <StackPanel Grid.Column="0" x:Name="stkEasyLocatePanel" Loaded="stkEasyLocatePanel_Loaded" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="0,0,0,28"></StackPanel>

    <Button Grid.Column="1" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,5,0,0" Panel.ZIndex="140" Height="20" Width="25" Background="Transparent" Content="&lt;&lt;">
         <Button.Triggers>
             <EventTrigger RoutedEvent="Button.Click">
                 <BeginStoryboard>
                     <Storyboard x:Name="HideStackPanel">
                         <DoubleAnimation Storyboard.TargetName="stkEasyLocatePanel" Storyboard.TargetProperty="Width" From="330" To="0" Duration="0:0:0.3">
                             <DoubleAnimation.EasingFunction>
                                 <PowerEase EasingMode="EaseIn"></PowerEase>
                             </DoubleAnimation.EasingFunction>
                         </DoubleAnimation>
                     </Storyboard>
                 </BeginStoryboard>
                 <BeginStoryboard>
                     <!--This part dosent work. The content collapses and shows on a single click. But I want it to happen on two clicks of same button-->
                     <Storyboard x:Name="ShowStackPanel"> 
                         <DoubleAnimation Storyboard.TargetName="stkEasyLocatePanel" Storyboard.TargetProperty="Width" From="0" To="330" Duration="0:0:0.3">
                             <DoubleAnimation.EasingFunction>
                                 <PowerEase EasingMode="EaseIn"></PowerEase>
                             </DoubleAnimation.EasingFunction>
                         </DoubleAnimation>
                     </Storyboard>
                 </BeginStoryboard>                                  
             </EventTrigger>
        </Button.Triggers>
    </Button>
</Grid>

【问题讨论】:

  • 您希望它在纯 xaml 中,您更喜欢使用 Triggers 还是 VisualStateManager?
  • 两者都可以。但我想知道为普通按钮执行此操作的解决方案。
  • 为什么普通按钮很重要?如有必要,您可以设置一个 Toggle 来模仿普通按钮的设计/行为,@AnjumSKhan 的答案是正确的选择。要仅使用普通按钮,您需要在后面的代码中处理条件表达式,在我看来,纯 xaml 方法在这种情况下就可以了。
  • 哦,知道了。是的切换按钮工作正常。我想知道是否有办法使用纯 xaml 使用普通按钮解决问题。但如果它涉及到代码,那么我猜 Toggle Button 是最好的方法。

标签: wpf xaml animation


【解决方案1】:

Button 替换为 ToggleButton ,并按原样使用下面的代码,看看这是否能解决您的问题。

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="0" Width="330" x:Name="stkEasyLocatePanel" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="0,0,0,28">
            <Image Source="C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg"/>
        </StackPanel>

        <ToggleButton Grid.Column="1" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,5,0,0" Panel.ZIndex="140" Height="20" Width="25" Background="Transparent" Content="&lt;&lt;">
            <ToggleButton.Triggers>               
                <EventTrigger RoutedEvent="ToggleButton.Unchecked">
                    <BeginStoryboard>
                        <Storyboard x:Name="HideStackPanel">
                            <DoubleAnimation Storyboard.TargetName="stkEasyLocatePanel" Storyboard.TargetProperty="Width" From="0" To="330" Duration="0:0:0.3">
                                <DoubleAnimation.EasingFunction>
                                    <PowerEase EasingMode="EaseIn"></PowerEase>
                                </DoubleAnimation.EasingFunction>
                            </DoubleAnimation>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Content">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="&lt;&lt;"  />
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="ToggleButton.Checked">
                <BeginStoryboard>
                        <!--This part dosent work. The content collapses and shows on a single click. But I want it to happen on two clicks of same button-->
                        <Storyboard x:Name="ShowStackPanel">
                            <DoubleAnimation Storyboard.TargetName="stkEasyLocatePanel" Storyboard.TargetProperty="Width" From="330" To="0" Duration="0:0:0.3">
                                <DoubleAnimation.EasingFunction>
                                    <PowerEase EasingMode="EaseIn"></PowerEase>
                                </DoubleAnimation.EasingFunction>
                            </DoubleAnimation>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Content">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="&gt;&gt;"  />
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </ToggleButton.Triggers>
        </ToggleButton>
    </Grid>

【讨论】:

  • 感谢我所做的。我想发布相同的代码作为解决方案。
【解决方案2】:

我建议在后面的代码中保留一个变量(类似于isCollaped),然后在后面的代码中连接到按钮的单击事件。在按钮的 click 方法中放置一些逻辑来隐藏或显示堆栈面板。 例如:

XAML

<StackPanel Grid.Column="0" x:Name="stkEasyLocatePanel" Loaded="stkEasyLocatePanel_Loaded" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="0,0,0,28"></StackPanel>

//Add the Click event.
<Button Grid.Column="1" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,5,0,0" Panel.ZIndex="140" Height="20" Width="25" Background="Transparent" Content="&lt;&lt;" Click=Button_Click>

代码隐藏

bool isCollapsed = false;
private void Button_Click(object sender, RoutedEventArgs e)
{
    if (isCollapsed)
    {
        //Create and run show Stackpanel animation
        //Change the content of the button to "<<"
    }
    else
    {
        //Create and run hide Stackpanel animation
        //Change the content of the button to ">>"
    }
    isCollapsed = !isCollapsed;
}

编辑

根据评论中的要求,这是一个关于如何从背后的代码制作动画的示例。

if (isCollapsed)
{
    DoubleAnimation showAnim = new DoubleAnimation();
    showAnim.Duration = TimeSpan.FromSeconds(0.3);
    showAnim.EasingFunction = new PowerEase() { EasingMode = EasingMode.EaseIn };
    showAnim.From = 330;
    showAnim.To = 0;
    stkEasyLocatePanel.BeginAnimation(StackPanel.WidthProperty, showAnim);
    //Change the content of the button to "<<"
}

【讨论】:

  • 如果您不知道如何从后面的代码中制作动画,请告诉我,我可以举个例子。
  • 嗨 Agustin 我使用了一个切换按钮,它就像一个普通按钮一样工作,但它有选中和未选中的事件,您可以在 xaml 中执行动画。如果证明对某人有帮助,我会发布我的代码。但是您能否发布代码如何从其背后的代码制作动画肯定会有所帮助。
  • 如果你使用切换按钮,那么你仍然可以连接到 Click 事件,而不是使用 isCollapsed 变量,你可以检查你的 ToggleButton IsChecked 属性(这个是,以防你仍然想尝试后面的代码)。
  • 感谢奥古斯丁的帮助。这应该有助于作为参考。
猜你喜欢
  • 1970-01-01
  • 2012-05-06
  • 2016-05-06
  • 2019-02-07
  • 2016-04-29
  • 2016-11-22
  • 1970-01-01
  • 2012-06-09
  • 1970-01-01
相关资源
最近更新 更多