职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 1313|回复: 0

关于C#代码实现ControlTemplate

[复制链接]
yoyo 发表于 2007-8-19 15:19 | 显示全部楼层 |阅读模式
  现在主流的控件模板和样式是引用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;
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-5-17 19:51 , Processed in 0.117829 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表