Wpf 使用淡入淡出动画使分组框出现和消失

Wpf 使用淡入淡出动画使分组框出现和消失,wpf,animation,visibility,fading,Wpf,Animation,Visibility,Fading,在下面的示例中,我不明白如何在选中/取消选中上方复选框时使我的“实时更新”组框出现/消失。我正在寻找XAML中的快速淡入/淡出效果,但我有点迷茫 <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.micr

在下面的示例中,我不明白如何在选中/取消选中上方复选框时使我的“实时更新”组框出现/消失。我正在寻找XAML中的快速淡入/淡出效果,但我有点迷茫

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    x:Class="WpfControlLibrary1.MainControl"
    x:Name="MultiVol" MinHeight="520.12" MinWidth="213">

    <Grid HorizontalAlignment="Stretch">
        <Grid.Background>
                <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                    <GradientStop Color="White" Offset="0.966"/>
                    <GradientStop Color="#FFD7D4FF"/>
                </LinearGradientBrush>
        </Grid.Background>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <StackPanel x:Name="LayoutRoot" HorizontalAlignment="Stretch"  Grid.Row="0">

            <GroupBox Margin="8,0" BorderBrush="#FF88B1D8" HorizontalAlignment="Stretch" Height="99">
                <GroupBox.Header>
                    <WrapPanel>
                    <Label Content="General" Background="#00000000" Foreground="#FF0033FF" FontWeight="Bold" FontFamily="/WpfControlLibrary1;component/Fonts/#Tahoma" />    
                    </WrapPanel>
                </GroupBox.Header>

                <UniformGrid Columns="2">
                    <Label Content="RICs" />
                    <TextBox AcceptsReturn="False" AcceptsTab="True" AllowDrop="True" IsTabStop="True" />
                    <Label Content="Preference" />
                    <UniformGrid VerticalAlignment="Center" Columns="2" Rows="1">
                        <RadioButton GroupName="preference" Content="Exotic" IsChecked="False" />
                        <RadioButton GroupName="preference" Content="Flow" IsChecked="True" />
                    </UniformGrid>
                    <Label Content="Live updates" />
                    <CheckBox IsChecked="True" VerticalAlignment="Center"/>
                </UniformGrid>  
            </GroupBox>
        </StackPanel>

        <DockPanel Grid.Row="1" HorizontalAlignment="Stretch">
            <GroupBox Margin="8,0" BorderBrush="#FF88B1D8" HorizontalAlignment="Stretch">
                <GroupBox.Header>
                    <WrapPanel>
                    <Label Content="Live updates" Background="#00000000" Foreground="#FF0033FF" FontWeight="Bold" FontFamily="/WpfControlLibrary1;component/Fonts/#Tahoma" />   
                    </WrapPanel>
                </GroupBox.Header>
                    <ListView MinHeight="100" Background="{x:Null}">
                        <ListView.View>
                            <GridView AllowsColumnReorder="False">
                                <GridViewColumn Header="RIC" />
                                <GridViewColumn Header="Last tick" />
                           </GridView>
                        </ListView.View>
                    </ListView>
            </GroupBox>         
        </DockPanel>

        </Grid>
</UserControl>


更新:我现在得到淡入,我试图通过添加触发器标记来添加淡出:

           <DockPanel.Style>
                <Style TargetType="{x:Type DockPanel}">
                    <Style.Triggers>
                        <Trigger Property="Visibility" Value="Visible">
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="00:00:0.5" From="0.0" To="1.0" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                        </Trigger>
                        <Trigger Property="Visibility" Value="Hidden">
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="00:00:0.5" From="1.0" To="0.0" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DockPanel.Style>

两个步骤。
第一个
可见性

  • 在参考资料中创建BooleantVisibilityConverter

    <UserControl.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    </UserControl.Resources>
    
第二个
动画

  • 为LiveUpdates DockPanel创建一个
    DockPanel.Style

    <DockPanel.Style>
        <Style TargetType="{x:Type DockPanel}">
            <Style.Triggers>
                <Trigger Property="Visibility" Value="Visible">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="00:00:01" From="0.0" To="1.0" />
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </DockPanel.Style>
    
    
    

非常好!感谢您的清晰解释:)这增加了淡入效果。我现在尝试添加渐进式淡出,但我尝试的不起作用。我用修改过的DockPanel更新了这个问题。淡出要困难得多,因为DockPanel不再显示淡出。这是一个。我想把它保存在XAML中。我的经验很少,但我很难相信这在XAML中是不可能的。以下是一些建议,可以做得更简单一些:
<DockPanel Grid.Row="1" HorizontalAlignment="Stretch"
           Visibility="{Binding ElementName=LiveUpdateCheckBox, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}">
<DockPanel.Style>
    <Style TargetType="{x:Type DockPanel}">
        <Style.Triggers>
            <Trigger Property="Visibility" Value="Visible">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="00:00:01" From="0.0" To="1.0" />
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
        </Style.Triggers>
    </Style>
</DockPanel.Style>