推广类网站,陈村网站开发,网站懒加载怎么做,网站注册了域名然后怎么做区别于 DataTemplate 数据模板#xff0c;ControlTemplate 是控件模板#xff0c;是为自定义控件的 Template 属性服务的#xff0c;Template 属性类型就是 ControlTemplate。
演示#xff0c;
自定义一个控件 MyControl#xff0c;包含一个字符串类型的依赖属性。
pub…区别于 DataTemplate 数据模板ControlTemplate 是控件模板是为自定义控件的 Template 属性服务的Template 属性类型就是 ControlTemplate。
演示
自定义一个控件 MyControl包含一个字符串类型的依赖属性。
public class MyControl : Control
{/// summary/// 获取或设置MyProperty的值/// /summary public string MyProperty{get (string)GetValue(MyPropertyProperty);set SetValue(MyPropertyProperty, value);}/// summary/// 标识 MyProperty 依赖属性。/// /summarypublic static readonly DependencyProperty MyPropertyProperty DependencyProperty.Register(nameof(MyProperty), typeof(string), typeof(MyControl), new PropertyMetadata(default(string)));static MyControl(){DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl)));}
}前端样式中设置一下 Template 属性它的值即 ControlTemplate,
UserControl.ResourcesStyle TargetType{x:Type local:MyControl}Setter PropertyTemplateSetter.ValueControlTemplate TargetType{x:Type local:MyControl}Grid BackgroundDeepPinkTextBlockHorizontalAlignmentCenterVerticalAlignmentCenterText{TemplateBinding MyProperty} //Grid/ControlTemplate/Setter.Value/Setter/Style/UserControl.Resources使用这个自定义控件设置其 MyProperty 属性值
local:MyControlWidth200Height40MyProperty我是自定义控件~ /显示效果