Animatint BlurEffect在一个样式中起作用但动画DropShadowEffect会导致错误?

时间:2017-05-25 17:24:53

标签: c# wpf xaml

我正在尝试动画我已应用于一个网格的DropShadowEffect和我已应用于另一个网格的BlurEffect

模糊动画似乎工作正常,这就是代码。

<Style x:Key="BluredGridStyle" TargetType="{x:Type Grid}">
            <Style.Triggers>
                <Trigger Property="extentions:GridExtention.IsBlured" Value="True">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="(Effect).Radius" From="0" To="10" Duration="0:0:0.2"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="(Effect).Radius" From="10" To="0" Duration="0:0:0.2"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.ExitActions>
                </Trigger>
            </Style.Triggers>
        </Style>

<Grid Background="{DynamicResource BrHostBackground}">
    <Grid extentions:GridExtention.IsBlured="{Binding BackgroundIsBlured}" Style="{StaticResource BluredGridStyle}">
        <Grid.Effect>
            <BlurEffect x:Name="BlurEffect" Radius="0"/>
        </Grid.Effect>
    </Grid>
</Grid>

但是当我尝试使用投影时,我会收到错误。这是我的初始代码。

        <Style x:Key="DropShaodowGridStyle" TargetType="{x:Type Grid}">
            <Style.Triggers>
                <Trigger Property="Visibility" Value="Visible">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="(Effect).BlurRadius" From="0" To="80" Duration="0:0:0.2"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="(Effect).BlurRadius" From="80" To="0" Duration="0:0:0.2"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.ExitActions>
                </Trigger>
            </Style.Triggers>
        </Style>

    <Grid Visibility="{Binding HardwareLoadingVisibility}">
        <Grid x:Name="grid" HorizontalAlignment="Center" VerticalAlignment="Center" Background="White" Width="550" Height="250" Style="{DynamicResource DropShaodowGridStyle}">
            <Grid.Effect>
                <DropShadowEffect ShadowDepth="0" Opacity="0.8"/>                    
            </Grid.Effect>               
        </Grid>
    </Grid>

这会产生以下错误

InvalidOperationException: Cannot resolve all property references in the property path '(Effect).BlurRadius'. Verify that applicable objects support the properties.

如果我将属性更改为(Effect).Radius,我会收到相同的错误。

我还尝试将属性更改为生成此错误的(UIElement.Effect).(DropShadowEffect.BlurRadius)

InvalidOperationException: 'Effect' property does not point to a DependencyObject in path '(0).(1)'.

1 个答案:

答案 0 :(得分:0)

看起来我找到了答案。

我必须为样式中的属性指定一个默认值。

<Style x:Key="DropShaodowBorderStyle" TargetType="{x:Type Border}">
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect ShadowDepth="0" BlurRadius="0"></DropShadowEffect>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="Visibility" Value="Visible">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="(UIElement.Effect).BlurRadius" From="0" To="80" Duration="0:0:2"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="(UIElement.Effect).BlurRadius" From="80" To="0" Duration="0:0:2"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.ExitActions>
                </Trigger>
            </Style.Triggers>
        </Style>