public void AddStateAtRuntime () { Rectangle rect = new Rectangle { Name = RootName }; VSMControl c = new VSMControl (); c.ApplyTemplate (); // Create a visual state in code which we will try to use to animate the template element called 'Grid' foreach (VisualStateGroup g in VisualStateManager.GetVisualStateGroups (c.TemplateGrid)) { VisualState s = new VisualState (); s.SetValue (FrameworkElement.NameProperty, "C"); s.Storyboard = CreateWidthStoryboard (RootName, 600, 700); g.States.Add (s); } // The template element "Grid" can't be found by the new storyboard CreateAsyncTest (c, () => { Assert.IsTrue (VisualStateManager.GoToState (c, "A", false), "#1"); Assert.Throws<InvalidOperationException> (() => VisualStateManager.GoToState (c, "C", false), "#2"); // Adding a new element called 'Grid' to the TestPanel does not work TestPanel.Children.Add (rect); Assert.Throws<InvalidOperationException> (() => VisualStateManager.GoToState (c, "C", false), "#3"); // The new element is not findable from the control Assert.IsNull (c.FindName (RootName), "#4"); Assert.AreSame (rect, TestPanel.FindName (RootName), "#5"); // Adding it to the template grid instead TestPanel.Children.Remove (rect); c.TemplateGrid.Children.Add (rect); // It's not findable from the panel, but it is from the Control Assert.AreSame (rect, c.FindName (RootName), "#6"); Assert.IsNull (TestPanel.FindName (RootName), "#7"); // Once it's findable from the control, the storyboard will resolve to the new element and succeed Assert.IsTrue (VisualStateManager.GoToState (c, "C", false), "#8"); }, () => { Assert.AreEqual (700, rect.Width, "#9"); // The template element 'Grid' is not changed Assert.IsTrue (Double.IsNaN (c.TemplateGrid.Width), "#10"); } ); }
public void TargetControlInTemplate() { Control c = new VSMTest.VSMControl(); Grid g = null; c.ApplyTemplate(); CreateAsyncTest(c, () => g = (Grid)VisualTreeHelper.GetChild(c, 0), () => Assert.IsTrue(VisualStateManager.GoToState(c, "A", false), "#1"), () => Assert.AreEqual(100, g.Width, "#2"), () => Assert.IsTrue(VisualStateManager.GoToState(c, "B", false), "#3"), () => Assert.AreEqual(200, g.Width, "#4"), () => Assert.IsTrue(VisualStateManager.GoToState(c, "A", true), "#5"), () => Assert.AreEqual(100, g.Width, "#6"), () => Assert.IsTrue(VisualStateManager.GoToState(c, "B", true), "#7"), () => Assert.AreEqual(200, g.Width, "#8") ); }
public void TargetControlInTemplate () { Control c = new VSMTest.VSMControl (); Grid g = null; c.ApplyTemplate (); CreateAsyncTest (c, () => g = (Grid) VisualTreeHelper.GetChild (c, 0), () => Assert.IsTrue (VisualStateManager.GoToState (c, "A", false), "#1"), () => Assert.AreEqual (100, g.Width, "#2"), () => Assert.IsTrue (VisualStateManager.GoToState (c, "B", false), "#3"), () => Assert.AreEqual (200, g.Width, "#4"), () => Assert.IsTrue (VisualStateManager.GoToState (c, "A", true), "#5"), () => Assert.AreEqual (100, g.Width, "#6"), () => Assert.IsTrue (VisualStateManager.GoToState (c, "B", true), "#7"), () => Assert.AreEqual (200, g.Width, "#8") ); }