在我的项目中,我有一个文件来为 WPF 创建大多数样式。我正在尝试设置 TextBlock 的背景并为其 ViewBox 设置动画以使其具有轮播效果。当应用程序加载时所有控件都存在时,我能够做的是成功。但是,在我添加与新触发器匹配的控件后,它会引发异常。这是我的代码
<Style TargetType="{x:Type TextBlock}">
<!-- Image Carousal -->
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}, AncestorLevel=4},Path=Name}" Value="WelcomeScreen_Master" />
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}, Path=Name}" Value="LayoutMainTitleGrid" />
<Condition Binding="{Binding RelativeSource={RelativeSource Self},Path=Name}" Value="Banner"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Width" Value="400" />
<Setter Property="Height" Value="146" />
<Setter Property="Foreground" Value="Transparent"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="Margin" Value="620,200,0,0" />
<Setter Property="Background">
<Setter.Value>
<ImageBrush
ImageSource="pack://application:,,,/LoremTheme;component/images/rotate.png"
Stretch="UniformToFill"
AlignmentY="Top"
AlignmentX="Right"
ViewboxUnits="Absolute" Viewbox="0,0,400,146"
ViewportUnits="Absolute" Viewport="0,0,300,109.5"
>
</ImageBrush>
</Setter.Value>
</Setter>
</MultiDataTrigger.Setters>
<MultiDataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<RectAnimation Storyboard.TargetProperty="Background.Viewbox"
To="0,0,400,146" BeginTime="0:0:5" Duration="0:0:0.1" />
</Storyboard>
</BeginStoryboard>
</MultiDataTrigger.EnterActions>
</MultiDataTrigger>
</Style.Triggers>
</Style>
如果我在代码中设置 TextBlock 的背景图像,则该代码有效,但它不是一个选项,因为应用程序将具有不同的样式文件。如果我删除从上面的样式我仍然可以设置没有动画的边框,这意味着 xaml 能够设置背景。但如果我保持它给出了以下例外。
---------------------------
Active Window : Exception
---------------------------
Method:Void VerifyPathIsAnimatable(System.Windows.PropertyPath)
System.InvalidOperationException: Cannot resolve all property references in the property path 'Background.Viewbox'. Verify that applicable objects support the properties.
at System.Windows.Media.Animation.Storyboard.VerifyPathIsAnimatable(PropertyPath path)
at System.Windows.Media.Animation.Storyboard.ClockTreeWalkRecursive(Clock currentClock, DependencyObject containingObject, INameScope nameScope, DependencyObject parentObject, String parentObjectName, PropertyPath parentPropertyPath, HandoffBehavior handoffBehavior, HybridDictionary clockMappings, Int64 layer)
我怎样才能使动画工作?现在我的假设是动画在样式设置背景之前开始,那么有没有办法延迟情节提要?还是有其他解决方案?