在XAML中的Rectangle内为ImageBrush设置不透明度

时间:2009-04-28 12:52:46

标签: xaml

我在一个矩形中有一个ImageBrush,我试图在加载时淡入,通过将不透明度从0增加到100。

这就是我所拥有的:

    <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>

引发的错误是:

'ImgBrush'名称在'System.Windows.Shapes.Rectangle'的名称范围内找不到

(在Storyboard.TargetName中指定了ImgBrush)

我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:0)

通过动画整个矩形的不透明度来解决这个问题,而不是试图为整个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>