WPF动画依赖问题 [英] WPF Animation dependency issue
问题描述
我刚开始学习 WPF 和 XAML,并尝试在我的机器上制作动画示例.基本上,当在相应的文本框中设置特定值时,标签背景应该会发生变化.问题是我收到以下错误:Background' 属性未指向路径 '(0).(1)
I just started to learn WPF and XAML and I tried to gete an animation sample to work on my machine. Basically, a label background should change when a specific value is set in the corresponding textbox. Issue is I get the following error: Background' property does not point to a DependencyObject in path '(0).(1)
这是 XAML:
<Window x:Class="WpfDataBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loc="clr-namespace:WpfDataBinding"
Title="MainWindow" Height="350" Width="264.828">
<Window.Resources>
<DataTemplate DataType="{x:Type loc:Person}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="210"/>
</Grid.ColumnDefinitions>
<TextBlock Name="nameLabel" Grid.Row="0" Grid.Column="0" Text="Name:" FontSize="14" Margin="3,3,0,0"/>
<TextBox Name="nameTextBox" Grid.Row="0" Grid.Column="1" Width="200" Text="{Binding Name}" FontSize="14" Margin="3" />
<TextBlock Name="ageLabel" Grid.Row="1" Grid.Column="0" Text="Age:" FontSize="14" Margin="3,3,0,0"/>
<TextBox Name="ageTextBox" Grid.Row="1" Grid.Column="1" Width="200" Text="{Binding Age}" FontSize="14" Margin="3"/>
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Age}" Value="21">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="ageLabel"
Storyboard.TargetProperty="(Label.Background).(SolidColorBrush.Color)"
To="Red" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Window.Resources>
<Grid Margin="0,0,0,-0.2" HorizontalAlignment="Left" Width="248">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ListBox ItemsSource="{Binding}" />
<StackPanel Grid.Row="2" Grid.ColumnSpan="2">
<Button Content="_Show.." Click="Button_Click"/>
<Button Content="_Age" Click="Button_Click_1"/>
</StackPanel>
</Grid>
谢谢
推荐答案
为了在 UI 元素的 Background
属性中为 SolidColorBrush 的 Color
属性设置动画,您需要先设置背景.TextBlock 的 Background
属性的默认值为 null
,因此没有任何动画.
In order to animate the Color
property of a SolidColorBrush in the Background
property of a UI Element, you need to first set the Background. The default value of the Background
property of a TextBlock is null
, so there is nothing to animate.
因此,在动画之前先设置背景:
So, first set a Background before animating it:
<TextBlock ...>
<TextBlock.Background>
</SolidColorBrush Color="Transparent"/>
</TextBlock.Background>
</TextBlock>
现在您可以编写TargetProperty
路径,如
Now your could write the TargetProperty
path like
Storyboard.TargetProperty="Background.Color"
或
Storyboard.TargetProperty="(TextBlock.Background).Color"
或
Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)"
甚至
Storyboard.TargetProperty="Background.(SolidColorBrush.Color)"
所有表达式都是等价的.PropertyPath XAML 语法 MSDN 上的文章.
All expressions are equivalent. The details are explained in the PropertyPath XAML Syntax article on MSDN.
这篇关于WPF动画依赖问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!