WPF动画依赖问题

时间:2016-01-15 12:24:18

标签: wpf xaml

我刚开始学习WPF和XAML,我试图将动画样本用于我的机器上。基本上,当在相应的文本框中设置特定值时,标签背景应该更改。 问题是我收到以下错误:背景' property不指向路径'(0)中的DependencyObject。(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>

谢谢

3 个答案:

答案 0 :(得分:3)

为了在UI元素的Color属性中为SolidColorBrush的Background属性设置动画,您需要先设置背景。 TextBlock的Background属性的默认值为null,因此无需动画。

因此,首先在设置动画之前设置背景:

<TextBlock ...>
    <TextBlock.Background>
        </SolidColorBrush Color="Transparent"/>
    </TextBlock.Background>
</TextBlock>

现在您可以像{/ p>一样编写TargetProperty路径了

Storyboard.TargetProperty="Background.Color"

Storyboard.TargetProperty="(TextBlock.Background).Color"

Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)"

甚至

Storyboard.TargetProperty="Background.(SolidColorBrush.Color)"

所有表达都是等价的。有关详细信息,请参阅MSDN上的PropertyPath XAML Syntax文章。

答案 1 :(得分:1)

根据this question您需要使用:

ColorAnimation Storyboard.TargetName="ageLabel"
Storyboard.TargetProperty="(TextBlock.Background).Color"
To="Red" Duration="0:0:1"/>

而不是(TextBlock.Background).(SolidColorBrush.Color),显然Background和SolidColorBrush是同一个对象。

答案 2 :(得分:0)

(Label.Background)。(SolidColorBrush.Color)行是问题所在。它必须是<ColorAnimation Duration="0" To="Red" Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)" Storyboard.TargetName="ageLabel" />