在ListBoxItem的ControlTemplate中设置VisualState内的Background属性?

时间:2018-02-12 15:05:45

标签: c# wpf wpf-style

我想将Background属性设置为LinearGradientBrush但似乎有很多约束,original code看起来像这样:

<VisualState x:Name="Selected">
    <Storyboard>
      <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                    Storyboard.TargetProperty="(Panel.Background).
        (SolidColorBrush.Color)">
        <EasingColorKeyFrame KeyTime="0"
                             Value="{StaticResource SelectedBackgroundColor}" />
      </ColorAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

我认为这可行:

<VisualState x:Name="Selected">
    <Storyboard>
        <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(Panel.Background).(LinearGradientBrush)">
            <EasingColorKeyFrame KeyTime="0">
                <EasingColorKeyFrame.Value>
                    <GradientStopCollection>
                        <GradientStop/>
                        <GradientStop/>
                    </GradientStopCollection>
                </EasingColorKeyFrame.Value>
            </EasingColorKeyFrame>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

但事实并非如此:

  

无法分配指定的值。以下类型是   预期:“颜色”。

     

属性“值”不支持类型的值   'GradientStopCollection'。

1 个答案:

答案 0 :(得分:1)

这应该有效:

<VisualState x:Name="Pressed">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background">
            <DiscreteObjectKeyFrame KeyTime="0">
                <DiscreteObjectKeyFrame.Value>
                    <LinearGradientBrush>
                        <GradientStop Color="Red"/>
                        <GradientStop Color="Green" Offset="1"/>
                    </LinearGradientBrush>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>