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'"); }