操作系统  办公  实用知识  设计  开发  WEB开发  移动开发  数据库  软件工程  网管  安全  管理  信息化  答疑  渠道 

关于C#代码实现ControlTemplate

2007-10-30 网友评论 0 条 点击进入论坛

  现在主流的控件模板和样式是引用XAML资源,不过感觉没有C#代码实现那么灵活,现介绍一下代码实现 ControlTemplate的方法:

  以下是引用片段:

  //控件呈现的显示内容1(这里为Image)

  FrameworkElementFactory fe = new FrameworkElementFactory(typeof(Image), "Image");

  BitmapImage bi = new BitmapImage();

  bi.BeginInit();

  bi.UriSource = new Uri(@"E:ChartControlHanYangChartControlImageMainBackground.jpg");

  bi.EndInit();

  fe.SetValue(Image.SourceProperty, bi);

  //控件呈现的显示内容2(这里为TextBox)

  FrameworkElementFactory fe2 = new FrameworkElementFactory(typeof(TextBox), "TextBox");

  fe2.SetValue(TextBox.WidthProperty,100.0);

  fe2.SetValue(TextBox.HeightProperty, 100.0);

  //把要呈现的显示内容封装起来

  FrameworkElementFactory f = new FrameworkElementFactory(typeof(Grid), "Grid");

  f.AppendChild(fe);

  f.AppendChild(fe2);

  //控件模板

  ControlTemplate ct = new ControlTemplate(typeof(Button));

  ct.VisualTree = f;

  //修改Button 的Template

  Button btn = new Button();

  btn.Template = ct;

已有 0 位对此文章感兴趣的网友发布了看法    
我来评两句 登录邮箱: 密码:
  匿名发表
今日推荐
技术文库(共有 46473 篇文章)
操作系统
办公软件
实用知识
网络管理
软件开发
WEB开发
软件工程
数据库
设计在线
信息安全
行业信息化
管理信息化
重点推荐
电子杂志订阅
点击电子杂志名称查看样刊
输入E-mail地址即可订阅
E-mail