tags:

views:

436

answers:

1

I have an ImageBrush within a Rectangle that I'm trying to fade in on load, by increasing the opacity from 0 to 100.

Here's what I have:

    <Rectangle HorizontalAlignment="Left" Width="200" Stroke="#FF000000" StrokeThickness="0" Fill="{DynamicResource MyImageBrush}" >            
        <Rectangle.Resources>
            <ImageBrush x:Name="ImgBrush" x:Key="MyImageBrush" ImageSource="tower.jpg" Stretch="UniformToFill" Opacity="0" >
            </ImageBrush>                
        </Rectangle.Resources>   
        <Rectangle.Triggers>
            <EventTrigger RoutedEvent="Rectangle.Loaded">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation x:Name="FadeIn" Storyboard.TargetName="ImgBrush"  Storyboard.TargetProperty="Opacity" From="0" To="100" Duration="0:0:5"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>                              
        </Rectangle.Triggers>
    </Rectangle>

The error that is thrown is:

'ImgBrush' name cannot be found in the name scope of 'System.Windows.Shapes.Rectangle'

(ImgBrush being specified in Storyboard.TargetName)

What can I do to resolve this?

A: 

Got around this by animating the opacity of the whole rectangle, rather than trying to animate the whole imageBrush.

     <Rectangle Name="LeftRectangle" Opacity="0" HorizontalAlignment="Left" Width="200" Stroke="#FF000000" StrokeThickness="0" Fill="{DynamicResource MyImageBrush}" >            
        <Rectangle.Resources>
            <ImageBrush x:Name="ImgBrush" x:Key="MyImageBrush" ImageSource="tower.jpg" Stretch="UniformToFill" Opacity="100" >
            </ImageBrush>                
        </Rectangle.Resources>   
        <Rectangle.Triggers>
            <EventTrigger RoutedEvent="Rectangle.Loaded">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation x:Name="FadeIn" Storyboard.TargetName="LeftRectangle"  Storyboard.TargetProperty="Opacity" From="0" To="100" Duration="0:4:5"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>                              
        </Rectangle.Triggers>
    </Rectangle>