Установка LinearGradientBrush вместо сплошного цвета для раскадровки.targetProperty в VisualState

#wpf #storyboard #lineargradientbrush

#wpf #раскадровка #lineargradientbrush

Вопрос:

Мне интересно, как я могу установить раскадровку.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:

Вставленный вами код анимирует определенные точки в существующем градиенте, который применяется в качестве свойства 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>

          <!-- ... -->
  

Комментарии:

1. Черт возьми, это действительно очевидно теперь, когда ты это сказал! Спасибо!