Sandeep Bansal
Sandeep Bansal

Reputation: 6394

Programmatic state changes not working in Silverlight

I'm trying to get states to change with the visual state manager through code.

I am using:

Microsoft.Expression.Interactivity.Core.ExtendedVisualStateManager.GoToElementState(this.LayoutRoot, "stateRegistration", true);

But it doesn't seem to want to work, I have create an event handler and also a listener but it there's no state changed when using that code.

Can anyone help me out.

XAML CODE (Code Snippet):

<Grid x:Name="LayoutRoot" Width="897" Height="699">
<VisualStateManager.VisualStateGroups>
<VisualState x:Name="stateRegistration">
                    <Storyboard>
                        <DoubleAnimation Duration="0" To="870" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="RegisterContent" d:IsOptimized="True"/>
                        <DoubleAnimation Duration="0" To="880" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="HomeContent" d:IsOptimized="True"/>
                        <DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="btnRegRegister" d:IsOptimized="True"/>
                        <DoubleAnimation Duration="0" To="-10" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)" Storyboard.TargetName="btnRegRegister" d:IsOptimized="True"/>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
</Grid>

Thanks.

Upvotes: 4

Views: 1594

Answers (1)

vortexwolf
vortexwolf

Reputation: 14037

There is strange behavior with the VisualStateManager: its states must be situated not in the control, but in the child control.

It means, that the GoToState method should be called with the this parameter instead of the this.LayoutRoot parameter, but definitions of state groups must be situated inside the Grid:

VisualStateManager.GoToState(this, "stateRegistration", true);

I don't know where to get the ExtendedVisualStateManager class so I use the default one.

Also if any animation of the state storyboard fails - all animations are cancelled. So try this code sample with two animations, it must work:

<Grid x:Name="LayoutRoot" Width="897" Height="699">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup>
            <VisualState x:Name="stateRegistration">
                <Storyboard>
                    <DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="btnRegRegister" d:IsOptimized="True"/>
                    <DoubleAnimation Duration="0" To="-10" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)" Storyboard.TargetName="btnRegRegister" d:IsOptimized="True"/>
                </Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <Button x:Name="btnRegRegister" Content="Some button" Click="btnRegRegister_Click" VerticalAlignment="Center" HorizontalAlignment="Center">
        <Button.RenderTransform>
            <CompositeTransform TranslateX="0" TranslateY="0" />
        </Button.RenderTransform>
    </Button>
</Grid>

Upvotes: 6

Related Questions