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

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

最佳答案

您粘贴的代码会为现有渐变中的某些停止点设置动画,该渐变被应用为背景属性。如果你只想用一个新画笔(恰好是一个线性渐变画笔)替换整个背景,你可以使用 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>

          <!-- ... -->

关于wpf - 将 LinearGradientBrush 而不是纯色设置为 VisualState 中的 storyboard.targetProperty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6024812/

相关文章:

c# - WPF ComboBox 到 ObservableCollection 的绑定(bind)

objective-c - 如何不将 segue 插入导航 Controller 堆栈?

具有硬颜色过渡的.NET画笔(类似于渐变)

ios - 关闭 Storyboard 上的模态视图 Controller

ios - 如何在 iOS 中使用 Storyboard保存和恢复状态?

c# - 如何更改 GDI+ LinearGradientBrush 的颜色?

wpf - 两端固定宽度渐变的LinearGradientBrush

wpf - 可调整大小的 WPF 网格根据实际大小在行和列之间插入空行

wpf - 为我的 window 改变 Aero 玻璃的颜色?

wpf - 从WPF中的数据库字段应用样式