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)

This is the 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>

Thank you

Octavian Epure
  • 1,019
  • 5
  • 19
  • 35

3 Answers3

3

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>

Now your could write the TargetProperty path like

Storyboard.TargetProperty="Background.Color"

or

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

or

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

or even

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

All expressions are equivalent. The details are explained in the PropertyPath XAML Syntax article on MSDN.

Clemens
  • 123,504
  • 12
  • 155
  • 268
1

As per this question you need to use:

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

rather than (TextBlock.Background).(SolidColorBrush.Color) as apparently Background and SolidColorBrush are the same object.

Community
  • 1
  • 1
spaceplane
  • 607
  • 1
  • 12
  • 27
0

(Label.Background).(SolidColorBrush.Color) line is the problem. It needs to be <ColorAnimation Duration="0" To="Red" Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)" Storyboard.TargetName="ageLabel" />

Radin Gospodinov
  • 2,313
  • 13
  • 14
  • I did this and I got a different error: 'System.Windows.Media.Animation.ColorAnimation' animation object cannot be used to animate property 'Background' because it is of incompatible type 'System.Windows.Media.Brush'. – Octavian Epure Jan 15 '16 at 12:36
  • I edited my answer ageLabel is of type TextBlock so you should use (TextBlock.Background).(SolidColorBrush.Color) – Radin Gospodinov Jan 15 '16 at 12:43
  • I used TextBlock.Background, and I get the first error showing up again. Strange thing is, I get this error when I raise an notification event for the textbox value change. If I comment the event raise, just to test, I no longer get the error but the animation does'nt work anyway. – Octavian Epure Jan 15 '16 at 12:51