WPF 关于C#代码实现ControlTemplate
2007-01-16 11:39
561 查看
现在主流的控件模板和样式是引用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;
//控件呈现的显示内容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;
相关文章推荐
- WPF 关于C#代码实现ControlTemplate
- WPF关于C#代码实现Template
- 以下是关于对称加密算法的C#实现代码,大家可以根据需要更改不同的算法,文中以Rijndael算法为例
- C# WPF下自定义ComboBox背景、边线、颜色等代码实现
- [WPF]使用C#代码实现DataTemplate
- C# WPF下自定义DataGrid背景、颜色、边线等代码实现
- [WPF]使用C#代码实现DataTemplate
- [C#]使用 C# 代码实现拓扑排序 dotNet Core WEB程序使用 Nginx反向代理 C#里面获得应用程序的当前路径 关于Nginx设置端口号,在Asp.net 获取不到的,解决办法 .Net程序员 初学Ubuntu ,配置Nignix 夜深了,写了个JQuery的省市区三级级联效果
- 关于C#代码实现ControlTemplate
- 关于C# Math 处理奇进偶不进的实现代码
- 关于c#连接ftp进行上传下载实现原理及代码
- C#实现关闭其他程序窗口或进程代码分享
- C#实现删除ACESS数据库内容并压缩ACESS数据库代码
- C#关于上传CSV的代码
- 基于C#的UDP协议的同步通信实现代码
- 转载:C# Word操作实现代码
- C#下的单例实现本文介绍了C#下关于单例的三种实现方法。 最普通的一种: 以下是引用片段: using System; public class Singleton { pr
- 关于dsp移相全桥dc-dc变换器实现代码
- MD5原理及c#代码实现