Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="VisualState"/> class.
        /// </summary>
        /// <param name="visualStateGroup">The visual state group that created the state.</param>
        /// <param name="visualStateName">The visual state's name.</param>
        internal VisualState(VisualStateGroup visualStateGroup, String visualStateName)
        {
            Contract.Require(visualStateGroup, "visualStateGroup");
            Contract.RequireNotEmpty(visualStateName, "name");

            this.visualStateGroup = visualStateGroup;
            this.qualifiedName    = String.Format("{0}.{1}", visualStateGroup.Name, visualStateName);
            this.name             = visualStateName;
        }
Example #2
0
        /// <summary>
        /// Invoked whenever application code or internal processes (such as a rebuilding layout pass) call
        /// ApplyTemplate. In simplest terms, this means the method is called just before a UI element displays
        /// in your app. Override this method to influence the default post-template logic of a class.
        /// </summary>
        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _detailsPresenter = (ContentPresenter)GetTemplateChild(PartDetailsPresenter);
            SetDetailsContent();

            SetMasterHeaderVisibility();
            OnDetailsCommandBarChanged();
            OnMasterCommandBarChanged();

            if (_loaded && _stateGroup == null)
            {
                _stateGroup = (VisualStateGroup)GetTemplateChild(WidthStates);
                if (_stateGroup != null)
                {
                    _stateGroup.CurrentStateChanged += OnVisualStateChanged;
                    _narrowState = GetTemplateChild(NarrowState) as VisualState;
                    UpdateView(true);
                }
            }
        }
Example #3
0
        public void VerifyIsEnabledChangeUpdatesVisualState()
        {
            RadioButtons     radioButtons      = null;;
            VisualStateGroup commonStatesGroup = null;

            RunOnUIThread.Execute(() =>
            {
                radioButtons = new RadioButtons();

                // Check 1: Set IsEnabled to true.
                radioButtons.IsEnabled = true;

                Content = radioButtons;
                Content.UpdateLayout();

                var radioButtonsLayoutRoot = (FrameworkElement)VisualTreeHelper.GetChild(radioButtons, 0);
                commonStatesGroup          = VisualStateManager.GetVisualStateGroups(radioButtonsLayoutRoot).First(vsg => vsg.Name.Equals("CommonStates"));

                Verify.AreEqual("Normal", commonStatesGroup.CurrentState.Name);

                // Check 2: Set IsEnabled to false.
                radioButtons.IsEnabled = false;
            });
            IdleSynchronizer.Wait();

            RunOnUIThread.Execute(() =>
            {
                Verify.AreEqual("Disabled", commonStatesGroup.CurrentState.Name);

                // Check 3: Set IsEnabled back to true.
                radioButtons.IsEnabled = true;
            });
            IdleSynchronizer.Wait();

            RunOnUIThread.Execute(() =>
            {
                Verify.AreEqual("Normal", commonStatesGroup.CurrentState.Name);
            });
        }
Example #4
0
        /// <summary>
        /// Gets the state transition storyboard for the specified state.
        /// </summary>
        /// <param name="visualStatesHost">
        /// FrameworkElement that defines the visual states
        /// (usually the root of the control's template).
        /// </param>
        /// <param name="stateGroupName">
        /// Name of the state group
        /// (speeds up the search for the state transition storyboard).
        /// </param>
        /// <param name="stateName">
        /// State to transition to.
        /// </param>
        /// <returns>The state transition storyboard.</returns>
        private static Storyboard GetStoryboardForVisualState(FrameworkElement visualStatesHost, string stateGroupName, string stateName)
        {
            Storyboard storyboard = null;

            var stateGroups             = VisualStateManager.GetVisualStateGroups(visualStatesHost);
            VisualStateGroup stateGroup = null;

            if (!string.IsNullOrEmpty(stateGroupName))
            {
                stateGroup = stateGroups.FirstOrDefault(g => g.Name == stateGroupName);
            }

            VisualState state = null;

            if (stateGroup != null)
            {
                state = stateGroup.States.FirstOrDefault(s => s.Name == stateName);
            }

            if (state == null)
            {
                foreach (var group in stateGroups)
                {
                    state = group.States.FirstOrDefault(s => s.Name == stateName);

                    if (state != null)
                    {
                        break;
                    }
                }
            }

            if (state != null)
            {
                storyboard = state.Storyboard;
            }

            return(storyboard);
        }
Example #5
0
        /// <summary>
        ///     Transitions a control's state.
        /// </summary>
        /// <param name="control">The control who's state is changing.</param>
        /// <param name="stateGroupsRoot">The element to get the VSG & customer VSM from.</param>
        /// <param name="stateName">The new state that the control is in.</param>
        /// <param name="useTransitions">Whether to use transition animations.</param>
        /// <returns>true if the state changed successfully, false otherwise.</returns>
        private static bool GoToStateCommon(Control control, FrameworkElement stateGroupsRoot, string stateName, bool useTransitions)
        {
            if (stateName == null)
            {
                throw new ArgumentNullException("stateName");
            }

            if (stateGroupsRoot == null)
            {
                return(false); // Ignore state changes if a stateGroupsRoot doesn't exist yet
            }

            IList <VisualStateGroup> groups = GetVisualStateGroupsInternal(stateGroupsRoot);

            VisualState      state = null;
            VisualStateGroup group = null;

            if (groups != null)
            {
                VisualStateManager.TryGetState(groups, stateName, out group, out state);
            }

            // Look for a custom VSM, and call it if it was found, regardless of whether the state was found or not.
            // This is because we don't know what the custom VSM will want to do. But for our default implementation,
            // we know that if we haven't found the state, we don't actually want to do anything.
            VisualStateManager customVsm = GetCustomVisualStateManager(stateGroupsRoot);

            if (customVsm != null)
            {
                return(customVsm.GoToStateCore(control, stateGroupsRoot, stateName, group, state, useTransitions));
            }
            else if (state != null)
            {
                return(GoToStateInternal(control, stateGroupsRoot, group, state, useTransitions));
            }

            return(false);
        }
        internal static void StartNewThenStopOld(this VisualStateGroup group, FrameworkElement element, params Storyboard[] newStoryboards)
        {
            var currentStoryboards = GetCurrentStoryboards(group);

            // Remove the old Storyboards. Remove is delayed until the next TimeManager tick, so the
            // handoff to the new storyboard is unaffected.
            for (int index = 0; index < currentStoryboards.Count; ++index)
            {
                if (currentStoryboards[index] == null)
                {
                    continue;
                }

                currentStoryboards[index].Remove(element);
            }
            currentStoryboards.Clear();

            // Start the new Storyboards
            for (int index = 0; index < newStoryboards.Length; ++index)
            {
                if (newStoryboards[index] == null)
                {
                    continue;
                }

                newStoryboards[index].Begin(element, HandoffBehavior.SnapshotAndReplace, true);

                // Hold on to the running Storyboards
                currentStoryboards.Add(newStoryboards[index]);

                // Silverlight had an issue where initially, a checked CheckBox would not show the check mark
                // until the second frame. They chose to do a Seek(0) at this point, which this line
                // is supposed to mimic. It does not seem to be equivalent, though, and WPF ends up
                // with some odd animation behavior. I haven't seen the CheckBox issue on WPF, so
                // commenting this out for now.
                // newStoryboards[index].SeekAlignedToLastTick(element, TimeSpan.Zero, TimeSeekOrigin.BeginTime);
            }
        }
        /// <summary>
        /// Gets the template parts for this ImagePreview and sets up
        /// references and target properties for such parts.
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _viewport         = this.GetTemplateChild(ViewportName) as ViewportControl;
            _imagePanel       = this.GetTemplateChild(ImagePanelName) as Canvas;
            Image             = this.GetTemplateChild(ImageName) as Image;
            _root             = this.GetTemplateRoot() as FrameworkElement;
            _commonStates     = this.GetTemplateVisualStateGroup(CommonStatesName);
            _disabledState    = this.GetTemplateVisualState(DisabledStateName);
            _normalState      = this.GetTemplateVisualState(NormalStateName);
            _previewTransform = this.GetTemplateChild(PreviewTransformName) as TranslateTransform;

            if (null != _normalState)
            {
                _previewStoryboard = _normalState.Storyboard;
            }

            if (null != _previewStoryboard)
            {
                _previewAnimation = _previewStoryboard.Children[0] as DoubleAnimation;
            }

            if (null != _viewport)
            {
                _viewport.SizeChanged += Viewport_SizeChanged;
            }

            if (null != Image)
            {
                ImageOpened += Image_Opened;
                if (null != _source)
                {
                    Image.Source = _source;
                    Image_Opened(Image, null);
                }
            }
        }
Example #8
0
        void addResources()
        {
            var visualState = new VisualState {
                Name    = "Selected",
                Setters =
                {
                    new Setter {
                        Property = BackgroundColorProperty,
                        Value    = Color.FromHex(Theme.Current.TodayCalendarBackgroundColor)
                    }
                }
            };

            var visualStateGroup = new VisualStateGroup {
                Name   = "CommonStates",
                States =
                {
                    visualState
                }
            };

            var setter = new Setter {
                Property = VisualStateManager.VisualStateGroupsProperty,
                Value    = new VisualStateGroupList {
                    visualStateGroup
                }
            };

            var style = new Style(typeof(CalendarCollectionViewCell))
            {
                Setters =
                {
                    setter
                }
            };

            Resources.Add(style);
        }
Example #9
0
        protected void OnDropIndicatorHeightChanged(DependencyPropertyChangedEventArgs e)
        {
            VisualStateGroup visualStateGroup = ReorderListBoxItem.GetVisualStateGroup((FrameworkElement)VisualTreeHelper.GetChild((DependencyObject)this, 0), "DropIndicatorStates");

            if (visualStateGroup == null)
            {
                return;
            }
            foreach (VisualState state in (IEnumerable)visualStateGroup.States)
            {
                foreach (Timeline child in (PresentationFrameworkCollection <Timeline>)state.Storyboard.Children)
                {
                    this.UpdateDropIndicatorAnimationHeight((double)e.NewValue, child);
                }
            }
            foreach (VisualTransition transition in (IEnumerable)visualStateGroup.Transitions)
            {
                foreach (Timeline child in (PresentationFrameworkCollection <Timeline>)transition.Storyboard.Children)
                {
                    this.UpdateDropIndicatorAnimationHeight((double)e.NewValue, child);
                }
            }
        }
Example #10
0
        public void When_SingleInactiveState()
        {
            Window.Current.SetWindowSize(new Size(5, 5));

            var sut = new AdaptiveTrigger {
                MinWindowHeight = 10, MinWindowWidth = 10
            };

            var state = new VisualState {
                Name = "activeState"
            };

            state.StateTriggers.Add(sut);

            var group = new VisualStateGroup();

            group.States.Add(state);

            group.CurrentState.Should().Be(null);

            Window.Current.SetWindowSize(new Size(15, 15));
            group.CurrentState.Should().Be(state);
        }
Example #11
0
        public void VisualElementGoesToCorrectStateWhenAvailable()
        {
            var    label = new Label();
            double targetBottomMargin = 1.5;

            var group = new VisualStateGroup();
            var list  = new VisualStateGroupList();

            var normalState = new VisualState {
                Name = NormalStateName
            };

            normalState.Setters.Add(new Setter {
                Property = View.MarginBottomProperty, Value = targetBottomMargin
            });

            list.Add(group);
            group.States.Add(normalState);

            VisualStateManager.SetVisualStateGroups(label, list);

            Assert.That(label.Margin.Bottom, Is.EqualTo(targetBottomMargin));
        }
        /// <summary>
        /// Invoked whenever application code or internal processes (such as a rebuilding layout pass) call
        /// ApplyTemplate. In simplest terms, this means the method is called just before a UI element displays
        /// in your app. Override this method to influence the default post-template logic of a class.
        /// </summary>
        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (!(inlineBackButton is null))
            {
                inlineBackButton.Click -= OnInlineBackButtonClicked;
            }

            inlineBackButton = (Button)GetTemplateChild(PART_BACK_BUTTON);
            if (!(inlineBackButton is null))
            {
                inlineBackButton.Click += OnInlineBackButtonClicked;
            }

            selectionStateGroup = (VisualStateGroup)GetTemplateChild(SELECTION_STATES);
            if (selectionStateGroup != null)
            {
                selectionStateGroup.CurrentStateChanged += OnSelectionStateChanged;
            }

            twoPaneView = (Microsoft.UI.Xaml.Controls.TwoPaneView)GetTemplateChild(PART_ROOT_PANE);
            if (!(twoPaneView is null))
            {
                twoPaneView.ModeChanged += OnModeChanged;
            }

            detailsPresenter = (ContentPresenter)GetTemplateChild(PART_DETAILS_PRESENTER);

            SetDetailsContent();

            SetMasterHeaderVisibility();
            OnDetailsCommandBarChanged();
            OnMasterCommandBarChanged();

            UpdateView(true);
        }
        // This extension method will generate the VisualStateGroups necessary to set a simple background color for
        // the Selected state on your DataTemplate.

        // See ConvenienceMethod.xaml.cs for a usage example

        public static DataTemplate WithSelectedBackgroundColor(this DataTemplate template, Color backgroundColor)
        {
            return(new DataTemplate(() => {
                var content = template.CreateContent() as VisualElement;

                var backColorSetter = new Setter {
                    Value = backgroundColor, Property = VisualElement.BackgroundColorProperty
                };

                var stateGroup = new VisualStateGroup {
                    Name = "Common", TargetType = typeof(Grid)
                };

                var normalState = new VisualState
                {
                    Name = "Normal",
                    TargetType = typeof(Grid),
                    Setters = { }
                };

                var selectedState = new VisualState
                {
                    Name = "Selected",
                    TargetType = typeof(Grid),
                    Setters = { backColorSetter }
                };

                stateGroup.States.Add(normalState);
                stateGroup.States.Add(selectedState);

                VisualStateManager.SetVisualStateGroups(content, new VisualStateGroupList {
                    stateGroup
                });

                return content;
            }));
        }
Example #14
0
        public void When_ControlTemplate_And_Animation()
        {
            var SUT = new ContentControl()
            {
                Tag = 42
            };
            DoubleAnimation anim = null;

            var template = new ControlTemplate(() => {
                var g = new Grid();

                var vg = new VisualStateGroup();
                var t1 = new VisualTransition();
                var sb = new Storyboard();
                anim   = new DoubleAnimation();
                anim.SetBinding(DoubleAnimation.ToProperty, new Binding()
                {
                    Path = "Tag", RelativeSource = RelativeSource.TemplatedParent
                });
                sb.Children.Add(anim);
                t1.Storyboard = sb;
                vg.Transitions.Add(t1);

                VisualStateManager.SetVisualStateGroups(g, new List <VisualStateGroup> {
                    vg
                });

                return(g);
            });

            SUT.Template = template;

            new Grid().Children.Add(SUT);             // This is enough for now, but the `SUT` should be in the visual tree for its template to get applied
            SUT.ApplyTemplate();

            Assert.IsNotNull(anim);
        }
        /// <summary>
        /// Invoked whenever application code or internal processes (such as a rebuilding layout pass) call
        /// ApplyTemplate. In simplest terms, this means the method is called just before a UI element displays
        /// in your app. Override this method to influence the default post-template logic of a class.
        /// </summary>
        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (_inlineBackButton != null)
            {
                _inlineBackButton.Click -= OnInlineBackButtonClicked;
            }

            _inlineBackButton = (Button)GetTemplateChild(PartBackButton);
            if (_inlineBackButton != null)
            {
                _inlineBackButton.Click += OnInlineBackButtonClicked;
            }

            _selectionStateGroup = (VisualStateGroup)GetTemplateChild(SelectionStates);
            if (_selectionStateGroup != null)
            {
                _selectionStateGroup.CurrentStateChanged += OnSelectionStateChanged;
            }

            _twoPaneView = (NavigationView.TwoPaneView)GetTemplateChild(PartRootPanel);
            if (_twoPaneView != null)
            {
                _twoPaneView.ModeChanged += OnModeChanged;
            }

            _detailsPresenter = (ContentPresenter)GetTemplateChild(PartDetailsPresenter);
            SetDetailsContent();

            SetListHeaderVisibility();
            OnDetailsCommandBarChanged();
            OnListCommandBarChanged();
            OnListPaneWidthChanged();

            UpdateView(true);
        }
Example #16
0
        /// <summary>
        /// Updates the drop-indicator height value for visual state and transition animations.
        /// </summary>
        /// <remarks>
        /// This is a workaround for the inability of visual states and transitions to do template binding
        /// in Silverlight 3. In SL4, they could bind directly to the DropIndicatorHeight property instead.
        /// </remarks>
        protected void OnDropIndicatorHeightChanged(DependencyPropertyChangedEventArgs e)
        {
            Panel            rootPanel = (Panel)VisualTreeHelper.GetChild(this, 0);
            VisualStateGroup vsg       = ReorderListBoxItem.GetVisualStateGroup(
                rootPanel, ReorderListBoxItem.DropIndicatorStateGroup);

            if (vsg != null)
            {
                foreach (VisualState vs in vsg.States)
                {
                    foreach (Timeline animation in vs.Storyboard.Children)
                    {
                        this.UpdateDropIndicatorAnimationHeight((double)e.NewValue, animation);
                    }
                }
                foreach (VisualTransition vt in vsg.Transitions)
                {
                    foreach (Timeline animation in vt.Storyboard.Children)
                    {
                        this.UpdateDropIndicatorAnimationHeight((double)e.NewValue, animation);
                    }
                }
            }
        }
Example #17
0
        public void VerifyIsEnabledChangeUpdatesVisualState()
        {
            var numberBox = SetupNumberBox();

            VisualStateGroup commonStatesGroup = null;

            RunOnUIThread.Execute(() =>
            {
                // Check 1: Set IsEnabled to true.
                numberBox.IsEnabled = true;
                Content.UpdateLayout();

                var numberBoxLayoutRoot = (FrameworkElement)VisualTreeHelper.GetChild(numberBox, 0);
                commonStatesGroup       = VisualStateManager.GetVisualStateGroups(numberBoxLayoutRoot).Cast <VisualStateGroup>().First(vsg => vsg.Name.Equals("CommonStates"));

                Verify.AreEqual("Normal", commonStatesGroup.CurrentState.Name);

                // Check 2: Set IsEnabled to false.
                numberBox.IsEnabled = false;
            });
            IdleSynchronizer.Wait();

            RunOnUIThread.Execute(() =>
            {
                Verify.AreEqual("Disabled", commonStatesGroup.CurrentState.Name);

                // Check 3: Set IsEnabled back to true.
                numberBox.IsEnabled = true;
            });
            IdleSynchronizer.Wait();

            RunOnUIThread.Execute(() =>
            {
                Verify.AreEqual("Normal", commonStatesGroup.CurrentState.Name);
            });
        }
Example #18
0
        static VisualStateGroupList CreateTestStateGroupsWithoutOnOffStates()
        {
            var stateGroups      = new VisualStateGroupList();
            var visualStateGroup = new VisualStateGroup {
                Name = CommonStatesName
            };
            var disabledState = new VisualState {
                Name = DisabledStateName
            };
            var focusedState = new VisualState {
                Name = FocusedStateName
            };
            var normalState = new VisualState {
                Name = NormalStateName
            };

            visualStateGroup.States.Add(disabledState);
            visualStateGroup.States.Add(focusedState);
            visualStateGroup.States.Add(normalState);

            stateGroups.Add(visualStateGroup);

            return(stateGroups);
        }
        /// <summary>
        /// Gets the VisualStateGroup with the given name, looking up the visual tree
        /// </summary>
        /// <param name="root">Element to start from</param>
        /// <param name="groupName">Name of the group to look for</param>
        /// <param name="searchAncestors">Whether or not to look up the tree</param>
        /// <returns>The group, if found</returns>
        public static VisualStateGroup GetVisualStateGroup(this FrameworkElement root, string groupName, bool searchAncestors)
        {
            IList groups = VisualStateManager.GetVisualStateGroups(root);

            foreach (object o in groups)
            {
                VisualStateGroup group = o as VisualStateGroup;
                if (group != null && group.Name == groupName)
                {
                    return(group);
                }
            }

            if (searchAncestors)
            {
                FrameworkElement parent = root.GetVisualParent();
                if (parent != null)
                {
                    return(parent.GetVisualStateGroup(groupName, true));
                }
            }

            return(null);
        }
Example #20
0
        private void PrepareVisualStateGroups()
        {
            IList <VisualStateGroup> visualStateGroups = VisualStateManager.GetVisualStateGroups(this);

            VisualStateGroup visualStateGroup = new VisualStateGroup();

            visualStateGroups.Add(visualStateGroup);

            VisualState fullScreenPortraitState = CreatePortraitState();

            visualStateGroup.States.Add(fullScreenPortraitState);

            VisualState fullScreenLandscapeState = CreateLandscapeState();

            visualStateGroup.States.Add(fullScreenLandscapeState);

            VisualState filledState = CreateFilledState();

            visualStateGroup.States.Add(filledState);

            VisualState snappedState = CreateSnappedState();

            visualStateGroup.States.Add(snappedState);
        }
Example #21
0
        public void GoToStateAction_Success()
        {
            var c = new TestGoToStateAction();

            c.StateName = "State1";
            var g = new Grid();
            var v = new VisualStateGroup {
                Name = "States"
            };
            var s1 = new VisualState {
                Name = "State1"
            };

            //var s1 = (VisualState)XamlReader.Parse("<VisualState x:Name=\"State1\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" />");
            v.States.Add(s1);
            VisualStateManager.GetVisualStateGroups(g).Add(v);

            c.Target = g;
            c.TestInvoke();

            Assert.AreEqual(g, c.Element);
            Assert.AreEqual("State1", c.ReceiveStateName);
            Assert.AreEqual(true, c.Success);
        }
        /// <summary>
        /// Starts the specified <paramref name="newStoryboards"/> on the <paramref name="element"/>.
        /// Afterwards, the currently running storyboards of the visual state group are stopped.
        /// </summary>
        /// <param name="group">The group on which the storyboards will be started/stopped.</param>
        /// <param name="element">The element on which the storyboards will be run.</param>
        /// <param name="newStoryboards">The new storyboards to be run.</param>
        public static void StartNewAndStopOldStoryboards(
            this VisualStateGroup group, FrameworkElement element, params Storyboard[] newStoryboards)
        {
            if (group == null)
            {
                throw new ArgumentNullException(nameof(group));
            }

            // Stop old storyboards before starting new ones.
            StopCurrentStoryboards(group, element);

            // Merge all of the passed Storyboards into one single master storyboard.
            // This fixes a bug in the WPF animation system which sometimes causes certain
            // storyboards to not fire a completed event.
            // See https://stackoverflow.com/questions/5002501/visualstatemanager-not-working-as-advertised
            // for details.
            Storyboard masterStoryboard = BuildMasterStoryboard(newStoryboards);

            if (masterStoryboard != null)
            {
                masterStoryboard.Begin(element, HandoffBehavior.SnapshotAndReplace, true);
                group.GetCurrentStoryboards().Add(masterStoryboard);
            }
        }
Example #23
0
        public ImagePreview()
        {
            this.DefaultStyleKey = typeof(ImagePreview);
            // Preview storyboard and animation.
            _previewAnimation  = new DoubleAnimation();
            _previewStoryboard = new Storyboard
            {
                RepeatBehavior = RepeatBehavior.Forever,
                AutoReverse    = true
            };
            _previewStoryboard.Children.Add(_previewAnimation);

            // Normal visual state and common state group.
            _commonStates = new VisualStateGroup();
            _commonStates.SetValue(FrameworkElement.NameProperty, CommonStatesName);
            _normalState = new VisualState();
            _normalState.SetValue(FrameworkElement.NameProperty, NormalStateName);
            _commonStates.States.Add(_normalState);
            _normalState.Storyboard = _previewStoryboard;
            VisualStateManager.GetVisualStateGroups(this).Add(_commonStates);

            Loaded   += ImagePreview_Loaded;
            Unloaded += ImagePreview_Unloaded;
        }
Example #24
0
        private void ScrollViewer_Loaded(object sender, RoutedEventArgs e)
        {
            var sv = sender as ScrollViewer;

            if (sv != null)
            {
                //sv.ScrollToHorizontalOffset(150);
                // Visual States are always on the first child of the control template
                FrameworkElement element = VisualTreeHelper.GetChild(sv, 0) as FrameworkElement;
                if (element != null)
                {
                    VisualStateGroup group = FindVisualState(element, "ScrollStates");
                    if (group != null)
                    {
                        group.CurrentStateChanging += new EventHandler <VisualStateChangedEventArgs>(group_CurrentStateChanging);
                    }
                    VisualStateGroup hgroup = FindVisualState(element, "HorizontalCompression");
                    if (hgroup != null)
                    {
                        hgroup.CurrentStateChanging += new EventHandler <VisualStateChangedEventArgs>(hgroup_CurrentStateChanging);
                    }
                }
            }
        }
Example #25
0
 private void SongItem_Unloaded(object sender, RoutedEventArgs e)
 {
     //卸载事件,删除引用
     if (_LinkedGroup != null)
     {
         _LinkedGroup.CurrentStateChanged -= Group_CurrentStateChanged;
         _LinkedGroup = null;
     }
     if (_LinkedCollection != null)
     {
         _LinkedCollection.CollectionChanged -= _Linkedlist_CollectionChanged;
         _LinkedCollection = null;
     }
     if (_LinkedList != null)
     {
         var listcontainer = VisualTreeHelper.GetParent(_LinkedList);
         if (listcontainer is Playlist)
         {
             (listcontainer as Playlist).ListTypeChanged -= SongItem_ListTypeChanged;
         }
         _LinkedList = null;
     }
     _LinkedItem = null;
 }
        // Have to wait to get the VisualStateGroup until the control has Loaded
        // If we try to get the VisualStateGroup in the OnApplyTemplate the
        // CurrentStateChanged event does not fire properly
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            if (DesignMode.DesignModeEnabled == false)
            {
                SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
                var frame = GetFrame();
                if (frame != null)
                {
                    frame.Navigating += OnFrameNavigating;
                }
            }

            if (_stateGroup != null)
            {
                _stateGroup.CurrentStateChanged -= OnVisualStateChanged;
            }

            _stateGroup = (VisualStateGroup)GetTemplateChild(WidthStates);
            _stateGroup.CurrentStateChanged += OnVisualStateChanged;

            _narrowState = GetTemplateChild(NarrowState) as VisualState;

            UpdateView(true);
        }
Example #27
0
        public PaginatedListBox()
        {
            Loaded += (s, e) =>
            {
                // prevent several hooking
                if (_alreadyHookedScrollEvents)
                {
                    return;
                }
                _alreadyHookedScrollEvents = true;

                // hook
                var sv = (ScrollViewer)FindElementRecursive(this, typeof(ScrollViewer));
                if (sv != null)
                {
                    var element = VisualTreeHelper.GetChild(sv, 0) as FrameworkElement;
                    if (element != null)
                    {
                        // get visual state
                        VisualStateGroup vgroup = FindVisualState(element, "VerticalCompression");
                        if (vgroup != null)
                        {
                            vgroup.CurrentStateChanging += (se, ev) =>
                            {
                                // on bottom compression, need to load next page
                                if (ev.NewState.Name == "CompressionBottom")
                                {
                                    _load();
                                }
                            }
                        }
                        ;
                    }
                }
            };
        }
Example #28
0
        public void ManagedVisualStates()
        {
            Rectangle r = new Rectangle {
                Name = "rect", Width = 0
            };
            VisualStateGroup group = new VisualStateGroup();

            group.SetValue(FrameworkElement.NameProperty, "group");

            VisualState vstate = new VisualState();

            vstate.SetValue(FrameworkElement.NameProperty, "state");

            DoubleAnimation anim = new DoubleAnimation {
                From = 100, To = 200, Duration = TimeSpan.FromSeconds(1)
            };

            anim.SetValue(Control.NameProperty, "ANIMATION");
            Storyboard.SetTargetName(anim, "rect");
            Storyboard.SetTargetProperty(anim, new PropertyPath("Width"));

            Storyboard sb = new Storyboard();

            sb.Children.Add(anim);
            vstate.Storyboard = sb;
            group.States.Add(vstate);

            Enqueue(() => TestPanel.Children.Add(r));
            Enqueue(() => VisualStateManager.GetVisualStateGroups((FrameworkElement)TestPanel.Parent).Add(group));
            Enqueue(() => Assert.AreEqual(0, r.Width, "#1"));
            Enqueue(() => Assert.IsTrue(VisualStateManager.GoToState((Control)TestPage, "state", false), "#2"));
            Enqueue(() => Assert.IsGreater(99, r.Width, "#3"));
            Enqueue(() => TestPanel.Children.Clear());
            Enqueue(() => VisualStateManager.GetVisualStateGroups(TestPage).Clear());
            EnqueueTestComplete();
        }
 public static VisualStateGroup Name(this VisualStateGroup visual, string name)
 {
     visual.Name = name;
     return(visual);
 }
Example #30
0
 protected override void OnApplyTemplate()
 {
     CommonStates = GetTemplatedChild <VisualStateGroup>(nameof(CommonStates));
     Update();
 }
 public static VisualStateGroup TargetType(this VisualStateGroup visual, Type type)
 {
     visual.TargetType = type;
     return(visual);
 }