WPF GroupBox 淡入淡出

本次使用动画模块实现GroupBox 区域淡出淡入,还在研究如何实现翻页模式的动画

csharp 复制代码
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="10,10,0,0">
        <Button Content="Show GroupBox 1" Click="ShowGroup1_Click" Margin="0,0,10,0" x:Name="buttonShowGroup1"/>
        <Button Content="Show GroupBox 2" Click="ShowGroup2_Click" x:Name="buttonShowGroup2"/>
    </StackPanel>

    <Grid Grid.Row="1">
        <GroupBox Name="groupBox1" Header="Group Box 1" Visibility="Visible">
            <TextBlock Text="This is Group Box 1" FontSize="24" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </GroupBox>
        <GroupBox Name="groupBox2" Header="Group Box 2" Visibility="Collapsed">
            <TextBlock Text="This is Group Box 2" FontSize="24" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </GroupBox>
    </Grid>

    <Grid.Triggers>
        <!-- Trigger for showing GroupBox 1 -->
        <EventTrigger RoutedEvent="Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="groupBox1" 
                                 Storyboard.TargetProperty="(UIElement.Opacity)" 
                                 From="0.0" To="1.0" Duration="0:0:0.5"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>

        <EventTrigger RoutedEvent="FrameworkElement.Loaded" SourceName="groupBox1">
            <BeginStoryboard>
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="groupBox1" 
                                             Storyboard.TargetProperty="(UIElement.Visibility)">
                        <!--<DiscreteObjectKeyFrame KeyTime="0:0:0.5" Value="Visible"/>-->
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.5">
                            <DiscreteObjectKeyFrame.Value>
                                <Visibility>Visible</Visibility>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>

        <!-- Trigger for hiding GroupBox 1 and showing GroupBox 2 -->
        <EventTrigger RoutedEvent="Button.Click" SourceName="buttonShowGroup2">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="groupBox1" 
                                 Storyboard.TargetProperty="(UIElement.Opacity)" 
                                 From="1.0" To="0.0" Duration="0:0:0.5"/>
                    <DoubleAnimation Storyboard.TargetName="groupBox2" 
                                 Storyboard.TargetProperty="(UIElement.Opacity)" 
                                 From="0.0" To="1.0" Duration="0:0:0.5"/>

                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="groupBox1" 
                                             Storyboard.TargetProperty="(UIElement.Visibility)">
                        <!--<DiscreteObjectKeyFrame KeyTime="0:0:0.5" Value="Collapsed"/>-->
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.5">
                            <DiscreteObjectKeyFrame.Value>
                                <Visibility>Collapsed</Visibility>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="groupBox2" 
                                             Storyboard.TargetProperty="(UIElement.Visibility)">
                        <!--<DiscreteObjectKeyFrame KeyTime="0:0:0.5" Value="Visible"/>-->
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.5">
                            <DiscreteObjectKeyFrame.Value>
                                <Visibility>Visible</Visibility>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>

        <!-- Trigger for hiding GroupBox 2 and showing GroupBox 1 -->
        <EventTrigger RoutedEvent="Button.Click" SourceName="buttonShowGroup1">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="groupBox2" 
                                 Storyboard.TargetProperty="(UIElement.Opacity)" 
                                 From="1.0" To="0.0" Duration="0:0:0.5"/>
                    <DoubleAnimation Storyboard.TargetName="groupBox1" 
                                 Storyboard.TargetProperty="(UIElement.Opacity)" 
                                 From="0.0" To="1.0" Duration="0:0:0.5"/>

                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="groupBox2" 
                                             Storyboard.TargetProperty="(UIElement.Visibility)">
                        <!--<DiscreteObjectKeyFrame KeyTime="0:0:0.5" Value="Collapsed"/>-->
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.5">
                            <DiscreteObjectKeyFrame.Value>
                                <Visibility>Collapsed</Visibility>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="groupBox1" 
                                             Storyboard.TargetProperty="(UIElement.Visibility)">
                        <!--<DiscreteObjectKeyFrame KeyTime="0:0:0.5" Value="Visible"/>-->
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.5">
                            <DiscreteObjectKeyFrame.Value>
                                <Visibility>Visible</Visibility>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Grid.Triggers>
</Grid>
相关推荐
精明的身影2 天前
深入WPF -- Dispatcher(补)
wpf
云中飞鸿2 天前
该如何进行WPF界面设计
wpf
云中飞鸿4 天前
WPF分哪几块
wpf
newbe365244 天前
我们如何使用 impeccable 优化前端界面设计与实现稳定性
前端·人工智能·分布式·github·aigc·wpf
Chris _data22 天前
WPF 学习第三天 — Modbus RTU 串口通信
hadoop·学习·wpf
布吉岛的石头22 天前
Java 程序员第 43 阶段05:微服务整合大模型,跨服务调用架构设计实战,Seata分布式事务实战
wpf
步步为营DotNet22 天前
基于.NET Aspire 实现云原生应用的高效监控与可观测性
云原生·.net·wpf
芒鸽23 天前
HarmonyOS 分布式开发实战:设备协同、数据共享与跨设备迁移
分布式·wpf·harmonyos
Volunteer Technology23 天前
Flink状态管理与容错(二)
大数据·flink·wpf
happyprince24 天前
07_verl-Trainer模块详解
人工智能·架构·wpf·强化学习