本文整理汇总了C#中System.Windows.Controls.Control.ApplyTemplate方法的典型用法代码示例。如果您正苦于以下问题:C# Control.ApplyTemplate方法的具体用法?C# Control.ApplyTemplate怎么用?C# Control.ApplyTemplate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Controls.Control
的用法示例。
在下文中一共展示了Control.ApplyTemplate方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BindingExtensionTemplatedParentTest
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);
}
示例2: TemplateApplyTest
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);
}
示例3: CheckDefaultMethods
static public void CheckDefaultMethods (Control c)
{
Assert.IsFalse (c.ApplyTemplate (), "ApplyTemplate");
Assert.IsFalse (c.Focus (), "Focus");
}
示例4: VisualInheritanceCore
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");
}
);
}
示例5: LogicalInheritanceCore
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");
}
);
}
示例6: BindingExtensionRelativeSourceTemplatedParentTest
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);
}
示例7: GetTemplateTextBox
private static TextBox GetTemplateTextBox(Control control)
{
control.ApplyTemplate();
return (TextBox) control.Template.FindName("PART_TextBox", control);
}