2016-09-15 269 views
0

我有一个StackPanel和一个按钮。初始化按钮的内容应该是< <当按钮被点击时,StackPanel的内容应该会崩溃,并且按钮文本应该变成>>其中崩溃正在发生。再次按下相同的按钮时,我希望显示的内容和文本应该是< <,我不确定如何操作。wpf动画隐藏和折叠按钮上的面板点击

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

你想在纯xaml中使用触发器还是使用VisualStateManager? –

+0

他们两个都很好。但我想知道为正常按钮做这个的解决方案。 – nikhil

+0

为什么正常按钮很重要?如果需要,可以设计一个切换来模拟正常按钮的设计/行为,@AnjumSKhan答案是一个正确的选项。要使用普通按钮,您需要在后面的代码中处理条件表达式,在我看来,纯xaml方法在这种情况下很好。 –

回答

1

更换ButtonToggleButton,并使用下面,因为它是代码,看是否能解决您的问题。

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

谢谢你,我做了什么。我想过发布相同的代码作为解决方案。 – nikhil

1

我会建议保持代码的变量后面(像isCollaped),然后在后面的代码转播到按钮的单击事件。在按钮的点击方法内放置一些逻辑来隐藏或显示堆栈面板。 例如:

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 "<<" 
} 
+0

让我知道如果你不知道如何从背后的代码做动画,所以我可以给你一个例子。 – Agustin0987

+0

嗨Agustin我使用了一个Togglebutton,它的工作原理就像一个正常的按钮,但它已经检查和未检查的事件,您可以在xaml中执行动画..如果它证明对某人有帮助,则发布我的代码。但是,您可以发布代码如何使用代码背后的代码进行动画制作,这将非常有帮助。 – nikhil

+0

如果你使用切换按钮,那么你仍然可以连接到Click事件,而不是使用'isCollapsed'变量,你可以检查你的'ToggleButton'' IsChecked'属性(这是,如果你还想要尝试后面的代码)。 – Agustin0987