Exemplo n.º 1
0
        public void DefaultTemplate_FindName()
        {
            ContentPresenter presenter = new ContentPresenter {
                Content = "Content"
            };

            presenter.Measure(Size.Empty);

            Grid grid = null;

            Assert.VisualChildren(presenter, "#1",
                                  new VisualNode <Grid>("#a", g => grid = g,
                                                        new VisualNode <TextBlock>("#b")
                                                        )
                                  );

            var child = (TextBlock)grid.Children[0];

            child.Name = "TestName";
            Assert.IsNull(child.FindName("TestName"), "#2");
            Assert.IsNull(grid.FindName("TestName"), "#3");
            Assert.IsNull(presenter.FindName("TestName"), "#4");

            grid.Children.Clear();
            TestPanel.Children.Add(child);
            Assert.IsNull(TestPanel.FindName("TestName"), "#5");
        }
Exemplo n.º 2
0
        void BasedOn_FindName_Core(bool useTwice)
        {
            Style s1      = new Style(typeof(Grid));
            Style s2      = new Style(typeof(Grid));
            Style basedon = new Style(typeof(Grid));

            s1.SetValue(FrameworkElement.NameProperty, "s1");
            s2.SetValue(FrameworkElement.NameProperty, "s2");
            basedon.SetValue(FrameworkElement.NameProperty, "basedon");

            s1.BasedOn = basedon;
            if (useTwice)
            {
                s2.BasedOn = basedon;
            }
            TestPanel.Children.Add(new Grid {
                Name = "Grid2", Style = s2
            });
            TestPanel.Children.Add(new Grid {
                Name = "Grid1", Style = s1
            });

            Assert.IsInstanceOfType <Style>(TestPanel.FindName("s1"), "#1");
            Assert.IsInstanceOfType <Style>(TestPanel.FindName("s2"), "#2");
            if (useTwice)
            {
                Assert.IsNull(TestPanel.FindName("basedon"), "#3");
            }
            else
            {
                Assert.AreSame(s1.BasedOn, TestPanel.FindName("basedon"), "#4");
            }
        }
Exemplo n.º 3
0
        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");
            }
                            );
        }
Exemplo n.º 4
0
        public void UseTwice()
        {
            string name = "SuperSecretBrush";
            Brush  b1   = new SolidColorBrush();

            b1.SetValue(FrameworkElement.NameProperty, name);

            var g1 = new Grid {
                Name = "g1", Background = b1
            };
            var g2 = new Grid {
                Name = "g2", Background = b1
            };

            TestPanel.Children.Add(g1);
            Assert.IsNull(TestPanel.FindName(name), "#1");

            TestPanel.Children.Add(g2);
            Assert.IsNull(TestPanel.FindName(name), "#2");
        }
Exemplo n.º 5
0
        public void SamePlacementTarget_MultipleTooltips()
        {
            // We can use the same object and name multiple times for PlacementTarget
            // as the Name is not registered.
            var placementTarget = new ToolTip {
                Name = "TooltipNameNotRegistered"
            };
            var target1 = new Grid();
            var target2 = new Grid();

            TestPanel.Children.Add(target1);
            TestPanel.Children.Add(target2);

            ToolTipService.SetPlacementTarget(target1, placementTarget);
            ToolTipService.SetPlacementTarget(target2, placementTarget);
            ToolTipService.SetPlacementTarget(TestPanel, new ToolTip {
                Name = "TooltipNameNotRegistered"
            });

            Assert.IsNull(TestPanel.FindName(placementTarget.Name), "#1");
        }
Exemplo n.º 6
0
        public void SameTooltipObjectForMultipleObjects_TooltipObject()
        {
            // We can set multiple tooltips with the same name without getting
            // a name collision as the Name is not registered.
            string name    = "TooltipNameNotRegistered";
            var    target1 = new Grid();
            var    target2 = new Grid();

            TestPanel.Children.Add(target1);
            TestPanel.Children.Add(target2);

            ToolTipService.SetToolTip(target1, new ToolTip {
                Name = name
            });
            ToolTipService.SetToolTip(target2, new ToolTip {
                Name = name
            });
            ToolTipService.SetToolTip(TestPanel, new ToolTip {
                Name = name
            });

            Assert.IsNull(TestPanel.FindName(name), "#1");
        }
Exemplo n.º 7
0
        public void DataTemplateNamescopeTest()
        {
            Grid         grid = (Grid)XamlReader.Load(@"
<Grid	xmlns=""http://schemas.microsoft.com/client/2007""
		xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
	<Grid.Resources>
		<DataTemplate x:Key=""Template"">
			<Grid x:Name=""A"">
				<Grid x:Name=""B"" />
			</Grid>
		</DataTemplate>
	</Grid.Resources>
</Grid>");
            DataTemplate t    = (DataTemplate)grid.Resources["Template"];
            Grid         root = (Grid)t.LoadContent();

            Assert.AreEqual(root, root.FindName("A"), "#1");
            Assert.IsInstanceOfType <Grid>(root.FindName("B"), "#2");

            TestPanel.Children.Add(root);
            Assert.IsNull(TestPanel.FindName("A"), "#3");
            Assert.IsNull(TestPanel.FindName("B"), "#4");
        }
Exemplo n.º 8
0
        public void UseTwice_ThenOnce()
        {
            string name  = "SuperSecretBrush";
            Brush  brush = new SolidColorBrush();

            brush.SetValue(FrameworkElement.NameProperty, name);

            var g1 = new Grid {
                Name = "g1", Background = brush
            };
            var g2 = new Grid {
                Name = "g2", Background = brush
            };

            TestPanel.Children.Add(g1);
            Assert.IsNull(TestPanel.FindName(name), "#1");

            g2.Background = null;
            Assert.IsNull(TestPanel.FindName(name), "#2");

            TestPanel.Children.Clear();
            TestPanel.Children.Add(g1);
            Assert.AreSame(brush, TestPanel.FindName(name), "#3");
        }