简体   繁体   English

基于网格可见性的WPF动画

[英]WPF Animation Based on Grid Visibility

I have a grid that starts collapsed. 我有一个开始崩溃的网格。 When the user clicks the button, the code behind sets visibility to to visible. 当用户单击按钮时,后面的代码将可见性设置为“可见”。 Then the buttons XAML trigger makes the grid grow from 0 to 1925 over 3 seconds. 然后,按钮XAML触发器使网格在3秒内从0增长到1925。

I was trying to figure out how in XAML to program the trigger to look for the visibility of the grid. 我试图弄清楚如何在XAML中对触发器进行编程,以查找网格的可见性。 Then depending on its state make the animation grow or shrink. 然后根据其状态使动画增长或缩小。 So based on the code below I would like to make the grid go from 1925 to 0 if the trigger determines the visibility is visible and 0 to 1925 if the grid is showing collapsed upon button click. 因此,根据下面的代码,如果触发器确定可见性可见,那么我希望使网格从1925变为0,如果单击按钮时网格显示为折叠,则网格从0变为1925。 Want to stay away from the code knowing whats going on in the UI. 希望远离代码,了解UI中发生的事情。 Which is why later I will bind grid visibility to a property. 这就是为什么以后我将网格可见性绑定到属性的原因。

Main Components of the XAML XAML的主要组件

    <Grid Name="gridDisplay" Background="AliceBlue" Visibility="Collapsed">

    </Grid>



    <Button 
        Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
        Width="32" Height="32" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Right" Background="White" Click="Button_Click">
        &gt;

        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation 
                            Storyboard.TargetName="gridDisplay"
                            Storyboard.TargetProperty="Width"
                            From="0" To="1925" Duration="0:0:3" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Button.Triggers>
    </Button>

Button Event 按钮事件

        //TODO: Bind grid to visibility property
        if (gridDisplay.Visibility == System.Windows.Visibility.Collapsed)
        {
            gridDisplay.Visibility = System.Windows.Visibility.Visible;
            (sender as Button).Content = "<";
        }
        else
        {
            gridDisplay.Visibility = System.Windows.Visibility.Collapsed;
            (sender as Button).Content = ">";
        }

Instead of using the triggers you can make a animation like this. 无需使用触发器,您可以制作这样的动画。

xaml : xaml:

  <Window.Resources>
        <Storyboard x:Key="open">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="gridDisplay">
                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="900"/>
            </DoubleAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="gridDisplay">
                <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Key="close">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="gridDisplay">
                <EasingDoubleKeyFrame KeyTime="0" Value="900"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="0"/>
            </DoubleAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="gridDisplay">
                <DiscreteObjectKeyFrame KeyTime="0:0:0.6" Value="{x:Static Visibility.Collapsed}"/>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>

buton event : 事件事件:

//TODO: Bind grid to visibility property
            if (gridDisplay.Visibility == System.Windows.Visibility.Collapsed)
            {
                var storyboard = this.Resources["open"] as Storyboard;
                storyboard.Begin();
                (sender as Button).Content = "<";
            }
            else
            {
                var storyboard = this.Resources["close"] as Storyboard;
                storyboard.Begin();
                (sender as Button).Content = ">";
            }

I hope this will help you. 我希望这能帮到您。 If you want to have a visible property you can have this: 如果要拥有可见属性,可以使用以下属性:

private bool _visibleProp;
public bool VisibleProp{get{return _visibleProp;} 
    set{_visibleProp = value; 
        if(value){var storyboard = this.Resources["open"] as Storyboard;
                storyboard.Begin(); }
        else{var storyboard = this.Resources["close"] as Storyboard;
                storyboard.Begin();}
          }}

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

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