2

我正在尝试为 WPF 表单中的内联警报框制作平滑的淡入/淡出动画。问题是即使属性未设置为触发器中预期的值,动画似乎也会运行。我在转换器方法中放置了一个断点,它从不返回除 Visibility.Hidden 之外的任何内容,但是 Visibility.Visible 上的触发器正在触发 Height 属性的动画(也可能会或可能不会触发不透明度的动画,但我不能'不知道,因为网格是 Visibility.Hidden)。

这是样式:

<Style TargetType="Grid" x:Key="AlertStyle">
    <Setter Property="Visibility" Value="Collapsed"/>
    <Style.Triggers>
        <Trigger Property="Visibility" Value="Visible">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                         Duration="00:00:02" 
                                         BeginTime="00:00:00" 
                                         To="1.0" />
                        <DoubleAnimation Storyboard.TargetProperty="Height"
                                         Duration="00:00:01" 
                                         BeginTime="00:00:00" 
                                         To="60.0"/>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
        </Trigger>
    </Style.Triggers>
</Style>

有问题的网格在这里:

<Grid DockPanel.Dock="Top" 
      Visibility="{Binding Path=Message, Converter={StaticResource NullToVis}}" 
      Style="{StaticResource AlertStyle}">
    <ContentPresenter Content="{Binding Path=Message, Mode=OneWay}"/>
</Grid>

价值转换器:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    return (value == null) ? Visibility.Hidden : Visibility.Visible;
}

当我向 <Style.Triggers> 元素添加第二个触发器时,动画都不会运行。

第二个触发器:

        <Trigger Property="Visibility" Value="Hidden">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                         Duration="00:00:01" 
                                         BeginTime="00:00:00" 
                                         To="0.0" />
                        <DoubleAnimation Storyboard.TargetProperty="Height"
                                         Duration="00:00:02" 
                                         BeginTime="00:00:00" 
                                         To="0.0"/>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
        </Trigger>

我以前从未尝试在 WPF 中做任何与动画相关的事情,我发现类似的样式遍布整个互联网,但无法成功修改任何内容以让它们完成我想要完成的事情。

4

1 回答 1

3

尝试这个:

<Style TargetType="Grid" x:Key="AlertStyle">
    <Setter Property="Visibility" Value="Collapsed"/>
    <Style.Triggers>
        <Trigger Property="Visibility" Value="Visible">
            <Trigger.EnterActions>
                <BeginStoryboard Name="Storyboard">
                    <Storyboard BeginTime="00:00:00">
                        <DoubleAnimation Storyboard.TargetProperty="Opacity" 
Duration="00:00:02" To="1.0" />
                        <DoubleAnimation Storyboard.TargetProperty="Height" 
Duration="00:00:01" To="60.0"/>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
            <Trigger.ExitActions>
                <StopStoryboard BeginStoryboardName="Storyboard" />
            </Trigger.ExitActions>
        </Trigger>
    </Style.Triggers>
</Style>

如果这不起作用,那么您可以尝试使用DataTrigger带有自定义Boolean值的 a ...您不会错得太远。

于 2013-08-29T14:42:27.577 回答