在WPF中动画化RadialGradientBrush的麻烦 [英] Trouble animating RadialGradientBrush in WPF
问题描述
我正在尝试为 RadialGradientBrush
在我的应用程序。我得到了超级有用的例外:
I'm trying to animate a RadialGradientBrush
in my application. I get the super helpful exception:
附加信息:'System.Windows.Style'值不能分配给属性'Style' System.Windows.Controls.Border'。 '[Unknown]'属性不指向路径'(0)。(1)。[0]。(2)'中的DependencyObject。标记文件'Eng.Modules.Core; component / system / grid / systemgridview.xaml'中的对象'System.Windows.Style'的错误行252位置51。
我知道我的 DoubleAnimation
的 Storyboard.TargetProperty 中的间接资源定位或部分路径限定是错误的, code>属性。任何想法?
I know it's something wrong with the indirect property targeting or partial path qualification in my DoubleAnimation
's Storyboard.TargetProperty
attribute. Any ideas?
<Border>
<Border.Resources>
<RadialGradientBrush x:Key="SomeBrush">
<RadialGradientBrush.GradientStops>
<GradientStop Color="White" Offset="0" />
<GradientStop Color="Gold" Offset="1" />
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</Border.Resources>
<Border.Style>
<Style TargetType="{x:Type Border}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Self}}" Value="True">
<Setter Property="Background" Value="{StaticResource SomeBrush}" />
<DataTrigger.EnterActions>
<BeginStoryboard x:Name="SomeStoryBoard">
<Storyboard>
<!-- RIGHT HERE -->
<DoubleAnimation
Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[0].(GradientStop.Offset)"
From="0" To="1" Duration="0:0:1"
RepeatBehavior="Forever"
AutoReverse="True" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="SomeStoryBoard" />
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
推荐答案
第一个问题是您正在设置背景您的 DataTrigger
中的渐变刷。因为这将在以后被应用,动画将无法找到画笔(因此没有找到依赖属性的隐含错误)。所以我做的第一件事是将边框的背景设置为手动刷新,而不是触发器。
The first problem is that you are setting the background to a gradient brush in your DataTrigger
. Because this gets applied later, the animation won't be able to find the brush (hence the cryptic error about not finding a dependency property). So the first thing I did was set the border's background to the brush manually, rather than in the trigger.
第二个问题是如何设置目标属性。您不需要使用括号语法,它的工作原理如下: Background.GradientStops [0] .Offset
。
The second problem was how you were setting up the target property. You don't need to use the parenthesis syntax- it works just fine as follows: Background.GradientStops[0].Offset
.
随着这些变化,边框动画完美;这是最后的标记:
With these changes the border animates perfectly; here is the final mark-up:
<Border>
<Border.Background>
<RadialGradientBrush>
<RadialGradientBrush.GradientStops>
<GradientStop Color="White" Offset="0" />
<GradientStop Color="Gold" Offset="1" />
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</Border.Background>
<Border.Style>
<Style TargetType="{x:Type Border}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Self}}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard x:Name="SomeStoryBoard">
<Storyboard>
<!-- RIGHT HERE -->
<DoubleAnimation
Storyboard.TargetProperty="Background.GradientStops[0].Offset"
From="0" To="1" Duration="0:0:1"
RepeatBehavior="Forever"
AutoReverse="True" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="SomeStoryBoard" />
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
这篇关于在WPF中动画化RadialGradientBrush的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!