mshwf
mshwf

Reputation: 7449

Setting Background property inside VisualState in the ControlTemplate of ListBoxItem?

I want to set the Background property to LinearGradientBrush but it seems it has so many constraints, the original code was looking like this:

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

I thought this might work:

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

But it doesn't:

The specified value cannot be assigned. The following type was expected: "Color".

Property 'Value' does not support values of type 'GradientStopCollection'.

Upvotes: 1

Views: 592

Answers (1)

Clemens
Clemens

Reputation: 128013

This should work:

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

Upvotes: 2

Related Questions