- 1
- 0
- 约6.78千字
- 约 14页
- 2023-08-03 发布于山东
- 举报
在Silverlight中开发一个自定义控件
引用:
本文主要叙述怎样在Silverlight2中开发一个自定义控件,我使用环境是VS2008Silverlight2Beta2。
一:创立Silverlight2类库项目,如下列图:
然后我们增添一个控件类,该能够继承自Control类,也能够继承自其他类比方ContentControl,ItemControl。我们继承自ContentControl,代码如
下:
复制内容到剪贴板
代码:
usingSystem;
usingSystem.Net;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingSystem.Windows.Documents;
usingSystem.Windows.Ink;
usingSystem.Windows.Input;
usingSystem.Windows.Media;
usingSystem.Windows.Media.Animation;
usingSystem.Windows.Shapes;
namespaceCarySLCustomControlLib
{
publicclassCarySLCustomControl:ContentControl
{}
}
其实现在已经做好了一个最简单的自定义控件,我们给给他一个控件模板就能够了。在Page.xaml的Grid中增添如下代码:
复制内容到剪贴板
代码:
custom:CarySLCustomControl
custom:CarySLCustomControl.Template
ControlTemplate
Gridx:Name=RootElement
Rectanglex:Name=BodyElementWidth=200Height=100
Fill=LavenderStroke=PurpleRadiusX=16RadiusY=16/
TextBlockText=CaryClickHorizontalAlignment=CenterVerticalAlignment=Center//Grid
/ControlTemplate
/custom:CarySLCustomControl.Template
/custom:CarySLCustomControl
效果如下列图:
二:创立控件模板
下面我们为控件提供一个默认的控件模板,像类库项目中增添增添Generic.xaml文件,代码如下:
复制内容到剪贴板
代码:
ResourceDictionary
xmlns=/winfx/2006/xaml/presentation
xmlns:x=/winfx/2006/xaml
xmlns:custom=clr-namespace:CarySLCustomControlLib;assembly=CarySLCustomControlLib
StyleTargetType=custom:CarySLCustomControl
SetterProperty=Template
Setter.Value
ControlTemplateTargetType=custom:CarySLCustomControl
Gridx:Name=RootElement
Rectanglex:Name=BodyElementWidth=200Height=100
Fill=LightCoralStroke=PurpleRadiusX=16RadiusY=16/
TextBlockText=CaryClickHorizontalAlignment=CenterVerticalAlignment=Center//Grid
/ControlTemplate
/Setter.Value
/Setter
/Style
/ResourceDictionary
向我们的控件类CarySLCustomControl的结构函数中增添如下代码:
复制内容到剪贴板
代码:
this.DefaultStyleKey=typeof(CarySLCustomControl);
在Page.xaml中我们只要要引用控件custom:CarySLCustomControl/,就能够达到上面同样的效果了。
三:模板绑定
我们在使用控件的时候都会做响应的属性设定,比方:
复制内容到剪贴板
代码:
custom:CarySLCustomControlWidth=500Height=500Background=LightGreen/
可是你现在做该设置是不会生效的,还仍旧使用控件模板的设定,我们能够在控件模板中经过使用
{TemplateBindingControlProperty}
的表
您可能关注的文档
最近下载
- 2025年无人机驾驶员执照欧洲(EASA)无人机法规对比专题试卷及解析.pdf VIP
- 2025年AWS认证DedicatedHosts支持的AMI类型与启动模式专题试卷及解析.pdf VIP
- 美容院服务合同(2025年最新规范).docx
- 上海市住建委《上海市建筑和装饰工程预算定额沪建标定〔2025〕1162号.docx VIP
- 2025年无人机驾驶员执照飞行监管责任体系专题试卷及解析.pdf VIP
- 2025年无人机驾驶员执照悬停失控应急处置流程专题试卷及解析.pdf VIP
- 2025年金融风险管理师利率风险结构易错题解析专题试卷及解析.pdf VIP
- 《最优控制与状态估计6》-公开课件.ppt VIP
- 标准图集-22G101-3 混凝土结构施工图平面整体表示方法制图规则和构造详图-独立基础、条形基础、筏形基础、桩基础.pdf VIP
- 2024黑龙江林业职业技术学院单招《数学》通关题库及答案详解【各地真题】.docx VIP
原创力文档

文档评论(0)