public void BindingExtensionTemplatedParentTest() { string text = @" <ControlTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='root'> <FrameworkElement x:Name='child' Width='{TemplateBinding FrameworkElement.Height}'/> </ControlTemplate>"; ControlTemplate template = XamlLoader.Load(XamlParser.Parse(text)) as ControlTemplate; Control control = new Control(); control.Template = template; control.ApplyTemplate(); FrameworkElement child = NameScope.GetTemplateNameScope(control).FindName("child") as FrameworkElement; Assert.AreEqual(control, child.TemplatedParent); control.Height = 100; Assert.AreEqual(100, child.Width); }
public void TemplateApplyTest() { string text = @" <ControlTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='root'> <FrameworkElement x:Name='child' Height='400'/> <ControlTemplate.Triggers> <Trigger Property='FrameworkElement.Width' Value='100'> <Setter Property='FrameworkElement.Height' Value='100'/> </Trigger> <Trigger Property='FrameworkElement.Width' Value='200'> <Setter Property='FrameworkElement.Height' Value='200'/> <Setter TargetName='child' Property='FrameworkElement.Height' Value='200'/> </Trigger> <EventTrigger RoutedEvent='FrameworkElement.Initialized'> <Setter Property='FrameworkElement.Height' Value='300'/> </EventTrigger> </ControlTemplate.Triggers> </ControlTemplate>"; ControlTemplate controlTemplate = XamlLoader.Load(XamlParser.Parse(text)) as ControlTemplate; Control control = new Control(); control.Width = 100; control.Template = controlTemplate; control.ApplyTemplate(); Assert.AreEqual(1, control.VisualChildren.Count()); FrameworkElement child1 = control.TemplateChild as FrameworkElement; Assert.IsNotNull(child1); FrameworkElement child2 = control.Template.FindName("child", control) as FrameworkElement; Assert.AreEqual(child1, child2); Assert.AreEqual(control, child1.TemplatedParent); Assert.AreEqual(400, child1.Height); Assert.AreEqual(BaseValueSource.ParentTemplate, child1.GetValueSource(FrameworkElement.HeightProperty).BaseValueSource); Assert.AreEqual(BaseValueSource.ParentTemplate, child1.GetBaseValueSource(FrameworkElement.HeightProperty)); Assert.AreEqual(100, control.Height); Assert.AreEqual(BaseValueSource.TemplateTrigger, control.GetValueSource(FrameworkElement.HeightProperty).BaseValueSource); Assert.AreEqual(BaseValueSource.TemplateTrigger, control.GetBaseValueSource(FrameworkElement.HeightProperty)); control.Width = 200; Assert.AreEqual(200, control.Height); Assert.AreEqual(BaseValueSource.TemplateTrigger, control.GetValueSource(FrameworkElement.HeightProperty).BaseValueSource); Assert.AreEqual(BaseValueSource.TemplateTrigger, control.GetBaseValueSource(FrameworkElement.HeightProperty)); Assert.AreEqual(200, child1.Height); Assert.AreEqual(BaseValueSource.ParentTemplateTrigger, child1.GetValueSource(FrameworkElement.HeightProperty).BaseValueSource); Assert.AreEqual(BaseValueSource.ParentTemplateTrigger, child1.GetBaseValueSource(FrameworkElement.HeightProperty)); control.RaiseEvent(new RoutedEventArgs(FrameworkElement.InitializedEvent, control)); Assert.AreEqual(300, control.Height); Assert.AreEqual(BaseValueSource.TemplateTrigger, control.GetValueSource(FrameworkElement.HeightProperty).BaseValueSource); Assert.AreEqual(BaseValueSource.TemplateTrigger, control.GetBaseValueSource(FrameworkElement.HeightProperty)); control.Template = null; control.ApplyTemplate(); Assert.AreEqual(Double.NaN, control.Height); }
static public void CheckDefaultMethods (Control c) { Assert.IsFalse (c.ApplyTemplate (), "ApplyTemplate"); Assert.IsFalse (c.Focus (), "Focus"); }
void VisualInheritanceCore (Control c1, DependencyProperty prop1, FrameworkElement c2, DependencyProperty prop2, object value, object other) { Assert.AreSame (c1, c2.Parent, "#parented"); Assert.AreNotEqual (value, c1.GetValue (prop1), "#1"); Assert.AreNotEqual (value, c2.GetValue (prop2), "#2"); Assert.IsNull (VisualTreeHelper.GetParent (c2), "#c2 no parent"); Assert.AreEqual (0, VisualTreeHelper.GetChildrenCount (c1), "#c1 no children"); c1.SetValue (prop1, value); Assert.IsNotNull (c1.GetValue (prop1), "#3"); Assert.IsNotNull (c2.GetValue (prop2), "#4"); Assert.AreNotEqual (c1.GetValue (prop1), c2.GetValue (prop2), "#5"); // Once we connect c2 to c1 via the template visual tree, it will inherit the value CreateAsyncTest (c1, () => { c1.ApplyTemplate (); }, () => { Assert.AreEqual (c1.GetValue (prop1), c2.GetValue (prop2), "#6"); // And if we change the value inside the template, it affects c2 var visualParent = (FrameworkElement) VisualTreeHelper.GetChild (c1, 0); visualParent.SetValue (prop1, other); Assert.AreEqual (c2.GetValue (prop2), other, "#7"); } ); }
void LogicalInheritanceCore (Control c1, DependencyProperty prop1, FrameworkElement c2, DependencyProperty prop2, object value, object other) { // Check that we inherit values even if the visual tree doesn't exist, // i.e. if we use logical inheritance. Assert.AreNotEqual (value, c1.GetValue (prop1), "#1"); Assert.AreNotEqual (value, c2.GetValue (prop2), "#2"); Assert.IsNull (VisualTreeHelper.GetParent (c2), "#c2 no parent"); Assert.AreEqual (0, VisualTreeHelper.GetChildrenCount (c1), "#c1 no children"); c1.SetValue (prop1, value); Assert.IsNotNull (c1.GetValue (prop1), "#3"); Assert.IsNotNull (c2.GetValue (prop2), "#4"); Assert.AreEqual (c1.GetValue (prop1), c2.GetValue (prop2), "#5"); // Now generate a visual tree with 'c2' at the end and check that the visual // tree doesn't affect which value is inherited CreateAsyncTest (c1, () => { c1.ApplyTemplate (); }, () => { var visualParent = (FrameworkElement) VisualTreeHelper.GetChild (c1, 0); visualParent.SetValue (prop1, other); Assert.AreEqual (c2.GetValue (prop2), value, "#6"); } ); }
public void BindingExtensionRelativeSourceTemplatedParentTest() { string text = @" <ControlTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='root'> <FrameworkElement x:Name='child' Width='{Binding Height, RelativeSource={RelativeSource TemplatedParent}}'/> </ControlTemplate>"; ControlTemplate template = XamlLoader.Load(XamlParser.Parse(text)) as ControlTemplate; Control control = new Control(); control.Template = template; control.ApplyTemplate(); FrameworkElement child = NameScope.GetTemplateNameScope(control).FindName("child") as FrameworkElement; Assert.AreEqual(control, child.TemplatedParent); control.Height = 100; Assert.AreEqual(100, child.Width); control.ClearValue(FrameworkElement.HeightProperty); child.SetValue(FrameworkElement.WidthProperty, 200.0, BaseValueSource.ParentTemplate); Assert.AreEqual(200, control.Height); }
private static TextBox GetTemplateTextBox(Control control) { control.ApplyTemplate(); return (TextBox) control.Template.FindName("PART_TextBox", control); }