How to update current state using VisualStateManager

I have a Silverlight 4 application where I have some states defined in XAML and use VisualStateManager.GoToState(this, "stateName", false)that works as expected.

However, the problem is that some state properties are associated with the user interface, so the user can customize things like color. This can be done when in this state (say, state A). Changes are not reflected until I switch to a different state, and then return to state A. The problem is not in the binding from the user interface to the basic properties (they have the expected values), but in the fact that the state needs to be updated - or at least this is my conclusion.

At first I just tried VisualStateManager.GoToState(this, "StateA", false), but found that "if the control is already in stateName state, GoToState takes no action, returns true . "

Then I tried without success, although this is really a bad solution:

VisualStateManager.GoToState(this, "StateB", false);
VisualStateManager.GoToState(this, "StateA", false);

Does anyone know how I can force an update or workaround?

UPDATE: I managed to get it to work with a combination of the answer below and using the following code when changing the dependency property. Storyboard.Stop()and Storyboard.Begin()seemed to take into account the new value of the properties set from the user interface.

if (VisualStateManager.GoToState(this, "StateA", false))
{
    VisualState stateA = (VisualState)VisualStateGroup.States[0];
    stateA.Storyboard.Stop();
    stateA.Storyboard.Begin();
}
+5
source share
2 answers

This code should work:

VisualStateManager.GoToState(this, "StateB", false);
VisualStateManager.GoToState(this, "StateA", false);

Silverlight/WPF . , Microsoft Silverlight.

, .

:

  • OnApplyTemplate , , . DependencyProperties , . , GoToState, , , .

  • , Property Change .

PropertyChanged :

 public static readonly DependencyProperty MinValueProperty =
        DependencyProperty.Register("MinValue", typeof(double), typeof(ScaleValueConverter), new PropertyMetadata(0.0d,OnMinValuePropertyChanged));
+4

UWP XAML VisualStateGroup:

<VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="PopupStates">
                <VisualState x:Name="Mobile">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="0"/>
                    </VisualState.StateTriggers>

:

VisualStateManager.GoToState(this, PopupStates.CurrentState.Name, false);
+1

All Articles