public void ChangeNameOfTemplatedItem() { MyControl c = (MyControl)System.Windows.Markup.XamlReader.Load(@" <clr:MyControl xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:clr=""clr-namespace:NameTortureTest;assembly=NameTortureTest""> <clr:MyControl.Template> <ControlTemplate> <Canvas x:Name=""Parent""> <Canvas x:Name=""Canvas""> <ContentPresenter /> </Canvas> </Canvas> </ControlTemplate> </clr:MyControl.Template> </clr:MyControl> "); c.Content = new object(); c.ApplyTemplate(); Canvas parent = (Canvas)c.GetTemplateChild("Parent"); Canvas canvas = (Canvas)c.GetTemplateChild("Canvas"); canvas.Name = "OtherCanvas"; Assert.IsTrue(canvas == c.GetTemplateChild("Canvas"), "Namescope is not updated"); parent.Children.Remove(canvas); testArea.Children.Add(canvas); Assert.IsNull(testArea.FindName("OtherCanvas"), "Name is not registered"); canvas.Name = "FinalName"; Assert.IsNull(testArea.FindName("FinalName"), "Name changes are not registered"); }
public void CustomControlInTemplate_Resources() { MyControl c = (MyControl)System.Windows.Markup.XamlReader.Load(@" <clr:MyControl xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:clr=""clr-namespace:NameTortureTest;assembly=NameTortureTest""> <clr:MyControl.Template> <ControlTemplate> <clr:CustomControl x:Name=""CustomControl"" /> </ControlTemplate> </clr:MyControl.Template> </clr:MyControl> "); c.ApplyTemplate(); // Get the grid at the root of CustomControl var fe = (FrameworkElement)VisualTreeHelper.GetChild(c, 0); fe = (FrameworkElement)VisualTreeHelper.GetChild(fe, 0); var resource = (FrameworkElement)fe.Resources ["Custom_Resource"]; Assert.IsNotNull(resource, "#1"); Assert.IsNotNull(resource.FindName("Custom_Resource2"), "#2"); Assert.IsNotNull(resource.FindName("CustomBorder"), "#3"); Assert.IsNotNull(resource.FindName("CustomBorder2"), "#4"); }
public void AddItemToCanvasInTemplate() { Rectangle rect = new Rectangle { Name = "Rect" }; MyControl c = (MyControl)System.Windows.Markup.XamlReader.Load(@" <clr:MyControl xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:clr=""clr-namespace:NameTortureTest;assembly=NameTortureTest""> <clr:MyControl.Template> <ControlTemplate> <Canvas x:Name=""Canvas""> <ContentPresenter /> </Canvas> </ControlTemplate> </clr:MyControl.Template> </clr:MyControl> "); c.Content = new object(); c.ApplyTemplate(); Canvas canvas = (Canvas)c.TemplateCanvas; canvas.Children.Add(rect); Assert.IsTrue(rect == c.FindName("Rect"), "Element should be findable from the control"); Assert.IsNull(c.GetTemplateChild("Rect"), "Rect is not added to the template namescope"); }
public void AddTemplateItemToNewSubtree() { MyControl c = (MyControl)System.Windows.Markup.XamlReader.Load(@" <clr:MyControl xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:clr=""clr-namespace:NameTortureTest;assembly=NameTortureTest""> <clr:MyControl.Template> <ControlTemplate> <Canvas x:Name=""Canvas""> <Rectangle x:Name=""Rect"" /> <ContentPresenter /> </Canvas> </ControlTemplate> </clr:MyControl.Template> </clr:MyControl> "); c.Content = new object(); c.ApplyTemplate(); Canvas canvas = (Canvas)c.TemplateCanvas; Rectangle r = (Rectangle)canvas.Children [0]; canvas.Children.Remove(r); testArea.Children.Add(r); Assert.IsTrue(r == c.GetTemplateChild("Rect"), "Rect is still in template namescope"); Assert.IsNull(testArea.FindName("Rect"), "Rect is not added to the new namescope"); }
public void CustomControlInTemplate() { MyControl c = (MyControl)System.Windows.Markup.XamlReader.Load(@" <clr:MyControl xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:clr=""clr-namespace:NameTortureTest;assembly=NameTortureTest""> <clr:MyControl.Template> <ControlTemplate> <clr:CustomControl x:Name=""CustomControl"" /> </ControlTemplate> </clr:MyControl.Template> </clr:MyControl> "); c.ApplyTemplate(); CustomControl custom = (CustomControl)c.GetTemplateChild("CustomControl"); custom.ApplyTemplate(); Assert.IsNotNull(custom, "Custom control should be findable"); Assert.IsNull(custom.FindName("LayoutRoot"), "Cannot FindName 'LayoutRoot' in CustomControl"); Assert.IsNull(c.GetTemplateChild("LayoutRoot"), "Cannot GetTemplateChild 'LayoutRoot' in MyControl"); Assert.IsNull(c.FindName("LayoutRoot"), "Cannot FindName 'LayoutRoot' in MyControl"); Assert.IsNull(custom.FindName("CustomBorder"), "Should not find 'CustomBorder' in the CustomControl"); var child = (FrameworkElement)VisualTreeHelper.GetChild(custom, 0); Assert.IsNotNull(child.FindName("CustomBorder"), "Should find 'CustomBorder' in the child of CustomControl"); Grid nestedGrid = (Grid)child.FindName("NestedGrid"); Assert.IsNotNull(nestedGrid.FindName("CustomBorder"), "Should find 'CustomBorder' in the child of CustomControl"); nestedGrid.Name = "NestedRenamedGrid"; Assert.IsNull(child.FindName("NestedGrid"), "Should not find 'NestedGrid' in the child of CustomControl (2)"); Assert.IsNotNull(child.FindName("NestedRenamedGrid"), "Should find 'NestedRenamedGrid' in the child of CustomControl"); MediaElement ctrlInNestedGrid = new MediaElement(); ctrlInNestedGrid.Name = "mediaElement"; nestedGrid.Children.Add(ctrlInNestedGrid); Assert.IsNotNull(child.FindName("mediaElement"), "Should find 'mediaElement' in the child of CustomControl"); Assert.IsNull(custom.FindName("mediaElement"), "Should not find 'mediaElement' in CustomControl"); Rectangle alice = new Rectangle { Name = "Alice" }; nestedGrid.Children.Add(alice); Assert.IsNull(c.FindName("Alice"), "c.FindName ('Alice')"); Assert.IsNotNull(nestedGrid.FindName("Alice"), "nestedGrid.FindName ('Alice')"); Assert.IsNotNull(child.FindName("Alice"), "child.FindName ('Alice')"); alice.Name = "Edward"; Assert.IsNull(c.FindName("Alice"), "c.FindName ('Alice') 2"); Assert.IsNull(nestedGrid.FindName("Alice"), "nestedGrid.FindName ('Alice') 2"); Assert.IsNull(child.FindName("Alice"), "child.FindName ('Alice') 2"); Assert.IsNull(c.FindName("Edward"), "c.FindName ('Edward')"); Assert.IsNotNull(nestedGrid.FindName("Edward"), "nestedGrid.FindName ('Edward')"); Assert.IsNotNull(child.FindName("Edward"), "child.FindName ('Edward')"); }
public void TemplateItemsCanFindEachOther() { MyControl c = (MyControl)System.Windows.Markup.XamlReader.Load(@" <clr:MyControl xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:clr=""clr-namespace:NameTortureTest;assembly=NameTortureTest""> <clr:MyControl.Resources> <Rectangle x:Name=""Resource"" /> </clr:MyControl.Resources> <clr:MyControl.Template> <ControlTemplate> <Canvas x:Name=""Parent""> <Canvas.Resources> <Rectangle x:Name=""Resource1"" /> </Canvas.Resources> <Canvas x:Name=""Canvas""> <Canvas.Resources> <Rectangle x:Name=""Resource2"" /> </Canvas.Resources> <ContentPresenter Name=""Presenter"" /> </Canvas> </Canvas> </ControlTemplate> </clr:MyControl.Template> </clr:MyControl> "); c.Content = new object(); c.ApplyTemplate(); var parent = (FrameworkElement)c.GetTemplateChild("Parent"); var canvas = (FrameworkElement)c.GetTemplateChild("Canvas"); var presenter = (FrameworkElement)c.GetTemplateChild("Presenter"); Assert.IsNotNull(canvas, "#1"); Assert.IsNotNull(presenter, "#2"); Assert.IsNotNull(canvas.FindName("Parent"), "#3"); Assert.IsNotNull(canvas.FindName("Presenter"), "#4"); Assert.IsNotNull(presenter.FindName("Parent"), "#5"); Assert.IsNotNull(presenter.FindName("Canvas"), "#6"); var resource = c.Resources ["Resource"] as FrameworkElement; var resource1 = parent.Resources ["Resource1"] as FrameworkElement; var resource2 = canvas.Resources ["Resource2"] as FrameworkElement; Assert.IsNotNull(resource.FindName("Resource"), "#7"); Assert.IsNull(resource.FindName("Resource1"), "#8"); Assert.IsNull(resource.FindName("Resource2"), "#9"); Assert.IsNull(resource1.FindName("Resource"), "#10"); Assert.IsNotNull(resource1.FindName("Resource1"), "#11"); Assert.IsNotNull(resource1.FindName("Resource2"), "#12"); Assert.IsNull(resource2.FindName("Resource"), "#13"); Assert.IsNotNull(resource2.FindName("Resource1"), "#14"); Assert.IsNotNull(resource2.FindName("Resource2"), "#15"); }
public void StoryboardTemplateItem() { MyControl c = (MyControl)System.Windows.Markup.XamlReader.Load(@" <clr:MyControl xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:clr=""clr-namespace:NameTortureTest;assembly=NameTortureTest""> <clr:MyControl.Template> <ControlTemplate> <Canvas x:Name=""Canvas""> </Canvas> </ControlTemplate> </clr:MyControl.Template> </clr:MyControl> "); c.Content = new object(); c.ApplyTemplate(); Canvas canvas = (Canvas)c.GetTemplateChild("Canvas"); Storyboard sb = new Storyboard(); DoubleAnimation anim = new DoubleAnimation(); Storyboard.SetTargetName(anim, "Canvas"); Storyboard.SetTargetProperty(anim, new PropertyPath("Width")); sb.Children.Add(anim); Assert.Throws <InvalidOperationException> (() => sb.Begin(), "Not in tree, cannot find 'Canvas'"); canvas.Resources.Add("Key", sb); Assert.Throws <InvalidOperationException> (() => sb.Begin(), "In template resources, cannot find 'Canvas'"); canvas.Resources.Clear(); c.Resources.Add("Key", sb); Assert.Throws <InvalidOperationException> (() => sb.Begin(), "In control resources, cannot find 'Canvas'"); }