public void Templated_Children_Should_Have_TemplatedParent_Set()
        {
            var target = new TemplatedControl
            {
                Template = new FuncControlTemplate(_ => new Decorator
                {
                    Child = new Panel
                    {
                        Children = new Controls
                        {
                            new TextBlock(),
                            new Border(),
                        }
                    }
                }),
            };

            target.ApplyTemplate();

            var templatedParents = target.GetVisualDescendents()
                                   .OfType <IControl>()
                                   .Select(x => x.TemplatedParent)
                                   .ToList();

            Assert.Equal(4, templatedParents.Count);
            Assert.True(templatedParents.All(x => x == target));
        }
        public void ApplyTemplate_Should_Create_Visual_Children()
        {
            var target = new TemplatedControl
            {
                Template = new FuncControlTemplate(_ => new Decorator
                {
                    Child = new Panel
                    {
                        Children = new Controls
                        {
                            new TextBlock(),
                            new Border(),
                        }
                    }
                }),
            };

            target.ApplyTemplate();

            var types = target.GetVisualDescendents().Select(x => x.GetType()).ToList();

            Assert.Equal(
                new[]
            {
                typeof(Decorator),
                typeof(Panel),
                typeof(TextBlock),
                typeof(Border)
            },
                types);
            Assert.Empty(target.GetLogicalChildren());
        }
        public void Nested_Templated_Control_Should_Not_Have_Template_Applied()
        {
            var target = new TemplatedControl
            {
                Template = new FuncControlTemplate(_ => new ScrollViewer())
            };

            target.ApplyTemplate();

            var child = (ScrollViewer)target.GetVisualChildren().Single();

            Assert.Empty(child.GetVisualChildren());
        }
        public void Templated_Child_Should_Have_Parent_Set()
        {
            var target = new TemplatedControl
            {
                Template = new FuncControlTemplate(_ => new Decorator())
            };

            target.ApplyTemplate();

            var child = (Decorator)target.GetVisualChildren().Single();

            Assert.Equal(target, child.Parent);
            Assert.Equal(target, child.GetLogicalParent());
        }
示例#5
0
        public static async void FocusAndBlinkBackColor(this TemplatedControl control, IBrush bgColor)
        {
            control.Focus();
            if (_BlinkedControls.Add(control))
            {
                var bc = control.Background;
                control.Background = bgColor;
                await Task.Delay(330);

                control.Background = bc;
                _BlinkedControls.Remove(control);
                //Task.Delay( 330 ).ContinueWith( _ => control.BackColor = bc, TaskScheduler.FromCurrentSynchronizationContext() );
            }
        }
示例#6
0
        public void Changing_Template_Should_Clear_Old_Templated_Childs_Parent()
        {
            var target = new TemplatedControl
            {
                Template = new FuncControlTemplate((_, __) => new Decorator())
            };

            target.ApplyTemplate();

            var child = (Decorator)target.GetVisualChildren().Single();

            target.Template = new FuncControlTemplate((_, __) => new Canvas());
            target.ApplyTemplate();

            Assert.Null(child.Parent);
        }
示例#7
0
        public void Template_Doesnt_Get_Executed_On_Set()
        {
            bool executed = false;

            var template = new ControlTemplate(_ =>
            {
                executed = true;
                return(new Control());
            });

            var target = new TemplatedControl
            {
                Template = template,
            };

            Assert.IsFalse(executed);
        }
示例#8
0
        public void Template_Result_Becomes_Visual_Child()
        {
            Control templateResult = new Control();

            var template = new ControlTemplate(_ =>
            {
                return(templateResult);
            });

            var target = new TemplatedControl
            {
                Template = template,
            };

            var children = ((IVisual)target).VisualChildren.ToArray();

            CollectionAssert.AreEqual(new[] { templateResult }, children);
        }
示例#9
0
        public void TemplatedParent_Is_Set_On_Generated_Template()
        {
            Control templateResult = new Control();

            var template = new ControlTemplate(_ =>
            {
                return(templateResult);
            });

            var target = new TemplatedControl
            {
                Template = template,
            };

            target.Measure(new Size(100, 100));

            Assert.AreEqual(target, templateResult.TemplatedParent);
        }
示例#10
0
        public void TemplatedParent_Is_Set_On_Generated_Template()
        {
            Control templateResult = new Control();

            var template = new ControlTemplate(_ =>
            {
                return(templateResult);
            });

            var target = new TemplatedControl
            {
                Template = template,
            };

            var children = ((IVisual)target).VisualChildren.ToArray();

            Assert.AreEqual(target, templateResult.TemplatedParent);
        }
示例#11
0
        public void Template_Result_Becomes_Visual_Child()
        {
            Control templateResult = new Control();

            var template = new ControlTemplate(_ =>
            {
                return(templateResult);
            });

            var target = new TemplatedControl
            {
                Template = template,
            };

            target.Measure(new Size(100, 100));
            var children = target.GetVisualChildren().ToList();

            CollectionAssert.AreEqual(new[] { templateResult }, children);
        }
示例#12
0
        public void Templated_Child_Should_Have_ApplyTemplate_Called_With_Logical_Then_Visual_Parent()
        {
            var target = new TemplatedControl
            {
                Template = new FuncControlTemplate(_ => new ApplyTemplateTracker())
            };

            target.ApplyTemplate();

            var child = (ApplyTemplateTracker)target.GetVisualChildren().Single();

            Assert.Equal(
                new[]
            {
                new Tuple <IVisual, ILogical>(null, target),
                new Tuple <IVisual, ILogical>(target, target),
            },
                child.Invocations);
        }
示例#13
0
        public void Template_Gets_Executed_On_Reading_Visual_Children()
        {
            bool executed = false;

            var template = new ControlTemplate(_ =>
            {
                executed = true;
                return(new Control());
            });

            var target = new TemplatedControl
            {
                Template = template,
            };

            var children = ((IVisual)target).VisualChildren.ToArray();

            Assert.IsTrue(executed);
        }
示例#14
0
        public void Template_Gets_Executed_On_Measure()
        {
            bool executed = false;

            var template = new FuncControlTemplate(_ =>
            {
                executed = true;
                return(new Control());
            });

            var target = new TemplatedControl
            {
                Template = template,
            };

            target.Measure(new Size(100, 100));

            Assert.True(executed);
        }
示例#15
0
        public void Templated_Child_Should_Be_NameScope()
        {
            var target = new TemplatedControl
            {
                Template = new FuncControlTemplate(_ => new Decorator
                {
                    Child = new Panel
                    {
                        Children = new Controls
                        {
                            new TextBlock(),
                            new Border(),
                        }
                    }
                }),
            };

            target.ApplyTemplate();

            Assert.NotNull(NameScope.GetNameScope((Control)target.GetVisualChildren().Single()));
        }
示例#16
0
 public static void SetBorderThickness(this TemplatedControl control, double thickness)
 {
     control.BorderThickness = new Thickness(thickness);
 }
示例#17
0
 public static void FocusAndBlinkBackColor(this TemplatedControl control) => control.FocusAndBlinkBackColor(Brushes.HotPink);
示例#18
0
 /// <summary>
 /// Get an control in the indicated template, this method can be within "protected overrive void OnTemplateApplied(e)" method only
 /// </summary>
 /// <typeparam name="T">Type of the Control to return</typeparam>
 /// <param name="templatedControl">TemplatedControl owner of the IndicatedControl</param>
 /// <param name="e">The TemplateAppliedEventArgs</param>
 /// <param name="name">The Name of the Control to return</param>
 /// <returns>a control with the indicated params</returns>
 public static T GetControl <T>(this TemplatedControl templatedControl,
                                TemplateAppliedEventArgs e,
                                string name)
     where T : AvaloniaObject
 => e.NameScope.Find <T>(name);
示例#19
0
 public static void SetSelectedControlSync(TemplatedControl control) => AppData.SelectedObject = control;
示例#20
0
文件: Page.xaml.cs 项目: dfr0/moon
		public void TemplatedControl ()
		{
			TemplatedControl c = new TemplatedControl { Name = "Custom" };
			c.ApplyTemplate ();

			Rectangle Bob = new Rectangle { Name = "Bob" };
			Storyboard resourceSB = c.Resources ["ResourceSB"] as Storyboard;
			Storyboard templateSb = (Storyboard) c.GetTemplateChild ("TemplateSB");
			Grid grid = c.TemplateGrid;
			Rectangle rect = (Rectangle) grid.FindName ("Rect");
			Rectangle fakeGrid = new Rectangle { Name = "Grid" };

			c.TemplateGrid.Children.Add (Bob);
			testArea.Children.Add (c);

			Assert.IsNull (testArea.FindName ("LayoutRoot"), "LayoutRoot.FindName (\"LayoutRoot\")");
			Assert.IsTrue (testArea.FindName ("Custom") == c, "LayoutRoot.FindName (\"Custom\")");
			Assert.IsNull (testArea.FindName ("Grid"), "LayoutRoot.FindName (\"Grid\")");
			Assert.IsNull (testArea.FindName ("Bob"), "LayoutRoot.FindName (\"Bob\")");
			Assert.IsNull (testArea.FindName ("Rect"), "LayoutRoot.FindName (\"Rect\")");

			Assert.IsNull (c.FindName ("LayoutRoot"), "c.FindName (\"LayoutRoot\")");
			Assert.IsTrue (c.FindName ("Custom") == c, "c.FindName (\"Custom\")");
			Assert.IsNull (c.FindName ("Grid"), "c.FindName (\"Grid\")");
			Assert.IsTrue (c.FindName ("Bob") == Bob, "c.FindName (\"Bob\")");
			Assert.IsNull (c.FindName ("Rect"), "c.FindName (\"Rect\")");

			Assert.IsNull (grid.FindName ("LayoutRoot"), "grid.FindName (\"LayoutRoot\")");
			Assert.IsNull (grid.FindName ("Custom"), "grid.FindName (\"Custom\")");
			Assert.IsTrue (grid.FindName ("Grid") == grid, "grid.FindName (\"Grid\")");
			Assert.IsNull (grid.FindName ("Bob"), "grid.FindName (\"Bob\")");
			Assert.IsTrue (grid.FindName ("Rect") == rect, "grid.FindName (\"Rect\")");

			Assert.IsNull (Bob.FindName ("LayoutRoot"), "Bob.FindName (\"LayoutRoot\")");
			Assert.IsTrue (Bob.FindName ("Custom") == c, "Bob.FindName (\"Custom\")");
			Assert.IsNull (Bob.FindName ("Grid"), "Bob.FindName (\"Grid\")");
			Assert.IsTrue (Bob.FindName ("Bob") == Bob, "Bob.FindName (\"Bob\")");
			Assert.IsNull (Bob.FindName ("Rect"), "Bob.FindName (\"Rect\")");

			Assert.IsNull (rect.FindName ("LayoutRoot"), "rect.FindName (\"LayoutRoot\")");
			Assert.IsNull (rect.FindName ("Custom"), "rect.FindName (\"Custom\")");
			Assert.IsTrue (rect.FindName ("Grid") == c.TemplateGrid, "rect.FindName (\"Grid\")");
			Assert.IsNull (rect.FindName ("Bob"), "rect.FindName (\"Bob\")");
			Assert.IsTrue (rect.FindName ("Rect") == rect, "rect.FindName (\"Rect\")");
		}
示例#21
0
 public async static Task SetSelectedControl(TemplatedControl control)
 {
     var th_sc = new Thread(() => AppData.SetSelectedControlSync(control));
     await Task.Run(th_sc.Start);
 }