开发者

Problem at the beginning of a ColorAnimation for a GradientStop in a VisualState

开发者 https://www.devze.com 2023-02-19 21:12 出处:网络
I have a very strange problem with a ColorAnimation. What I want to do is pretty simple (I think): I have a Rectangle with a GradientBrush as a background. This rectangle has different VisualStates th

I have a very strange problem with a ColorAnimation. What I want to do is pretty simple (I think): I have a Rectangle with a GradientBrush as a background. This rectangle has different VisualStates that change the colors of the gradient. I want to put an animation for the transition between the states. My problem is that when I change the state, the animation does not start with the current color, but with the default color.

Here is a code snippet that can help you reproduce this:

<Window x:Class="TestColorAnimation.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="350" Width="525">

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="30" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <Rectangle Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" x:Name="rect">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup>
                <VisualStateGroup.Transitions>
                    <VisualTransition GeneratedDuration="0:0:0.5"/>
                </VisualStateGroup.Transitions>
                <VisualState x:Name="OrangeState">
                    <Storyboard>
                        <ColorAnimation Storyboard.TargetProperty="Color"
                                        Storyboard.TargetName="stop1"
                                        To="Orange" />
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="RedState">
                    <Storyboard>
                        <ColorAnimation Storyboard.TargetProperty="Color"
                                        Storyboard.TargetName="stop1"
                                        To="Red" />
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Rectangle.Fill>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop x:Name="stop1" Color="LightGray" Offset="0" />
                <GradientStop x:Name="stop2" Color="LightGray" Offset="1" />
            </LinearGradientBrush>
        </Rectangle.Fill>
    </Rectangle>

    <Button Grid.Row="1"开发者_Go百科 Grid.Column="0" Content="Go to orange"
     x:Name="orangeButton" Click="orangeButton_Click" />
    <Button Grid.Row="1" Grid.Column="1" Content="Go to red"
     x:Name="redButton" Click="redButton_Click" />
</Grid>
</Window>

The event handlers just call for the state change:

private void orangeButton_Click(object sender, RoutedEventArgs e)
{
    VisualStateManager.GoToElementState(this.rect, "OrangeState", true);
}

private void redButton_Click(object sender, RoutedEventArgs e)
{
    VisualStateManager.GoToElementState(this.rect, "RedState", true);
}

In this case, if I go to the orange state, the animation goes from gray to orange. But then, when I go to the red state, the animation goes from gray to red. I would like (and expected) it to go from orange to red.

I have tried to use a SolidColorBrush instead of the LinearGradientBrush and the transitions are happening as expected. I also tried to use the storyboards outside of the VisualStates (but with a LinearGradientBrush) and it also works as expected. The only situation in which I have reproduced this issue is the one in the example: LinearGradientBrush and ColorAnimation in a VisualState.

Am I doing something wrong? Is what I want possible?


Animating the LinearGradients will work if you change the target to the GradientStop Color directly:

<VisualState x:Name="OrangeState">
    <Storyboard>
        <ColorAnimation 
            Storyboard.TargetProperty = "(Rectangle.Fill).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)"
            Storyboard.TargetName="rect"
            To="Orange" />
    </Storyboard>
</VisualState>
<VisualState x:Name="RedState">
    <Storyboard>
        <ColorAnimation 
            Storyboard.TargetProperty = "(Rectangle.Fill).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)"
            Storyboard.TargetName="rect"
            To="Red" />
    </Storyboard>
</VisualState>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号