none
ExtendedVisualStateManager does not work in code behind

    Question

  • ExtendedVisualStateManager works in xaml.

    But when I try to replicate it in code I works, but without animation.

    <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="Group1" ei:ExtendedVisualStateManager.UseFluidLayout="True">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition GeneratedDuration="0:0:0.5"/>
                                    </VisualStateGroup.Transitions>
                                    <VisualState x:Name="G1Shown">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="NotificationFlyout">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <System:Double>100</System:Double>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="G1Hidden" />
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <VisualStateManager.CustomVisualStateManager>
                                <ei:ExtendedVisualStateManager/>
                            </VisualStateManager.CustomVisualStateManager>
                            <i:Interaction.Behaviors>
                                <ei:DataStateBehavior
                                    Binding="{Binding Path=DataContext.ShowNotificationFlyout, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
                                    Value="True"
                                    TrueState="G1Shown"
                                    FalseState="G1Hidden"/>
                            </i:Interaction.Behaviors>

    Code Behind

    var frameShown = new DiscreteObjectKeyFrame(100.0d, KeyTime.FromTimeSpan(TimeSpan.Zero));
    var framesShown = new ObjectAnimationUsingKeyFrames();
    framesShown.KeyFrames.Add(frameShown);

    VisualState shownState = new VisualState();
    shownState.Name = SHOWN_STATE;
    shownState.Storyboard = new Storyboard();
    shownState.Storyboard.Children.Add(framesShown);

    Storyboard.SetTargetProperty(framesShown, new PropertyPath("(FrameworkElement.Height)"));
    Storyboard.SetTarget(framesShown, flyout);

    var frameHidden = new DiscreteObjectKeyFrame(0.0d, KeyTime.FromTimeSpan(TimeSpan.Zero));
    var framesHidden = new ObjectAnimationUsingKeyFrames();
    framesHidden.KeyFrames.Add(frameHidden);

    VisualState hiddenState = new VisualState();
    hiddenState.Name = HIDDEN_STATE;

    VisualStateGroup group = new VisualStateGroup();
    group.Name = "group1";
    ExtendedVisualStateManager.SetUseFluidLayout(group, true);

    group.States.Add(shownState);
    group.States.Add(hiddenState);

    group.Transitions.Add(
    new VisualTransition() { GeneratedDuration = new Duration(TimeSpan.FromMilliseconds(100)) });

    VisualStateManager.SetCustomVisualStateManager(firstChild, new ExtendedVisualStateManager());

    var groups =
    VisualStateManager.GetVisualStateGroups(firstChild);

    groups.Add(group);                                 

    var dataStateBehavior = new DataStateBehavior();
    dataStateBehavior.Value = true;
    dataStateBehavior.TrueState = SHOWN_STATE;
    dataStateBehavior.FalseState = HIDDEN_STATE;

    var binding = new Binding();
    binding.FallbackValue = false;
    binding.Mode = BindingMode.OneWay;
    binding.Path = new PropertyPath($"{FrameworkElement.DataContextProperty.Name}.{BusyAndFlyoutControl.ShowNotificationFlyoutProperty.Name}");
    binding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = typeof(Window) };
    binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    BindingOperations.SetBinding(dataStateBehavior, DataStateBehavior.BindingProperty, binding);

    BehaviorCollection Behaviors = Interaction.GetBehaviors(firstChild);
    Behaviors.Add(dataStateBehavior);



    Thursday, July 19, 2018 10:49 AM