需要在Windows Phone 7中转换矩形的颜色

时间:2010-08-02 19:02:04

标签: c# silverlight silverlight-4.0 windows-phone-7

我有一个矩形,我有一个故事板:

<Storyboard x:Key="PressAndHoldColorBar">
        <ColorAnimationUsingKeyFrames Duration="0:0:10" FillBehavior="Stop" Storyboard.TargetName="rectWarning" Storyboard.TargetProperty="Fill">
            <LinearColorKeyFrame KeyTime="0:0:0" Value="Green" />
            <LinearColorKeyFrame KeyTime="0:0:7" Value="Yellow" />
            <LinearColorKeyFrame KeyTime="0:0:10" Value="Red" />
        </ColorAnimationUsingKeyFrames>
    </Storyboard>

<Rectangle Height="100" HorizontalAlignment="Left" Margin="12,328,0,0" Name="rectWarning" Stroke="Black" StrokeThickness="1" VerticalAlignment="Top" Width="200">

我希望在按下另一个按钮时发生这种颜色更改,所以我试图以编程方式调用代码:

s = (Storyboard)this.Resources["PressAndHoldColorBar"];
        s.Begin();

我把它缩小到第一个片段中的Storyboard.TargetName和TargetProperty(我想)。我只需要帮助找出如何使其完全发挥作用。

1 个答案:

答案 0 :(得分:3)

想出来:

<Storyboard x:Name="PressAndHoldColorBar">
        <ColorAnimationUsingKeyFrames Duration="0:0:10" FillBehavior="Stop" Storyboard.TargetName="rectWarning" 
                                      Storyboard.TargetProperty="(Fill).(SolidColorBrush.Color)">
            <LinearColorKeyFrame KeyTime="0:0:0" Value="Green" />
            <LinearColorKeyFrame KeyTime="0:0:7" Value="Yellow" />
            <LinearColorKeyFrame KeyTime="0:0:10" Value="Red" />
        </ColorAnimationUsingKeyFrames>
    </Storyboard>

你必须使用(填充)。(SolidColorBrush.Color)。它不喜欢(Shape.Fill)。