将LinearGradientBrush而不是纯色设置为VisualState中的storyboard.targetProperty

时间:2011-05-17 00:04:34

标签: wpf storyboard lineargradientbrush

我想知道如何将Storyboard.TargetProperty设置为LinearGradientBrush而不是纯色。 我是VisualStates的新手,所以如果我的问题没有提供足够的信息,请告诉我。大多数情况下,我只想设置渐变而不是纯色,无法弄清楚如何。谢谢你的帮助。我的工作是在http://msdn.microsoft.com/en-us/library/ms753328.aspx找到的一个例子。

 <VisualState x:Name="Disabled">
    <Storyboard>
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).
                  (GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                            Storyboard.TargetName="Border">
                <EasingColorKeyFrame KeyTime="0"
                                     Value="{StaticResource DisabledControlDarkColor}" />
              </ColorAnimationUsingKeyFrames>
              <ColorAnimationUsingKeyFrames
                  Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                                            Storyboard.TargetName="Border">
                <EasingColorKeyFrame KeyTime="0"
                                     Value="{StaticResource DisabledForegroundColor}" />
              </ColorAnimationUsingKeyFrames>
              <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).
                  (GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                            Storyboard.TargetName="Border">
                <EasingColorKeyFrame KeyTime="0"
                                     Value="{StaticResource DisabledBorderDarkColor}" />
              </ColorAnimationUsingKeyFrames>
            </Storyboard>
          </VisualState>

1 个答案:

答案 0 :(得分:10)

您粘贴的代码会在现有渐变中为某些停靠点设置动画,该渐变将作为Background属性应用。如果您只想用新画笔(恰好是LinearGradientBrush)替换整个背景,您可以使用ObjectAnimationUsingKeyFrames来完成。

文档甚至给出了这个例子:

      <Storyboard>

        <!-- ObjectAnimationUsingKeyFrames is used to animate properties that take
             an object as a value. This animation lasts for 4 seconds using 3 KeyFrames which
             swap different brush objects at regular intervals, making the background of the Page
             change. -->
        <ObjectAnimationUsingKeyFrames
          Storyboard.TargetProperty="Background"
          Duration="0:0:4" RepeatBehavior="Forever">
        <ObjectAnimationUsingKeyFrames.KeyFrames>

          <!-- Note: Only discrete interpolation (DiscreteObjectKeyFrame) is available for 
          use with ObjectAnimationUsingKeyFrames which merely swaps objects according to
          a specified timeline. Other types of interpolation are too problematic to apply
          to objects.  -->

          <!-- Using a DiscreteObjectKeyFrame, the Page Background suddenly changes 
               to a LinearGradientBrush after the first second of the animation. -->
          <DiscreteObjectKeyFrame KeyTime="0:0:1">
            <DiscreteObjectKeyFrame.Value>
              <LinearGradientBrush>
                <LinearGradientBrush.GradientStops>
                  <GradientStop Color="Yellow" Offset="0.0" />
                  <GradientStop Color="Orange" Offset="0.5" />
                  <GradientStop Color="Red" Offset="1.0" />
                </LinearGradientBrush.GradientStops>
              </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
          </DiscreteObjectKeyFrame>

          <!-- ... -->