使用代码浏览WPF控件模版
纯代码创建,不需要创建界面,创建WPF工程后,直接复制代码就可以使用。
当你手头没有Blend,又不记得以下这段代码,但是又想浏览控件模版的时候,就可以直接复制拿来用了。
public partial class MainWindow : Window
{
ListBox lbox;
TextBox tbox;
Grid grid;
public MainWindow()
{
InitializeComponent();
InitializeControl();
LoadControlTemplate(); } /// <summary>
/// 创建界面
/// </summary>
private void InitializeControl()
{
grid = new Grid();
ColumnDefinition col1 = new ColumnDefinition();
ColumnDefinition col2 = new ColumnDefinition();
col1.Width = GridLength.Auto;
grid.ColumnDefinitions.Add(col1);
grid.ColumnDefinitions.Add(col2); //List
lbox = new ListBox();
lbox.SelectionChanged += (a, b) => { ShowControlTemplate(); };
//TextBox
tbox = new TextBox();
tbox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
tbox.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto; grid.Children.Add(lbox);
grid.Children.Add(tbox); Grid.SetColumn(lbox, );
Grid.SetColumn(tbox, ); this.Content = grid;
} private void LoadControlTemplate()
{
Type type = typeof(System.Windows.Controls.Control);
List<Type> controlType = new List<Type>();
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly(typeof(System.Windows.Controls.Control)); foreach (Type item in assembly.GetTypes())
{
if (item.IsSubclassOf(type) && !item.IsAbstract && item.IsPublic)
{
controlType.Add(item);
}
}
lbox.ItemsSource = controlType;
} private void ShowControlTemplate()
{
try
{
Type type = (Type)lbox.SelectedItem;
System.Reflection.ConstructorInfo info = type.GetConstructor(Type.EmptyTypes);
Control control = (Control)info.Invoke(null);
control.Visibility = Visibility.Collapsed;
grid.Children.Add(control);
ControlTemplate template = control.Template; System.Xml.XmlWriterSettings setting = new System.Xml.XmlWriterSettings();
setting.Indent = true;
StringBuilder sb = new StringBuilder();
System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(sb, setting);
System.Windows.Markup.XamlWriter.Save(template, writer);
tbox.Text = sb.ToString();
grid.Children.Remove(control); }
catch (Exception ex)
{
tbox.Text = ex.Message;
}
}
}
截图:

使用代码浏览WPF控件模版的更多相关文章
- 如何通过代码设置WPF控件的字体,颜色
this.lbTips.Foreground = new SolidColorBrush(Colors.Black); this.lbTips.FontFamily = new FontFamily( ...
- 解决 CefSharp WPF控件不能使用输入法输入中文的问题(代码已提交到 github)
首先,本文所有 代码已经提交到github,需要的可以直接从github获取:https://github.com/starts2000/CefSharp,希望可以帮助到有需要的朋友们. CEF 简介 ...
- c#字符串加载wpf控件模板代码 - 简书
原文:c#字符串加载wpf控件模板代码 - 简书 ResourceManager resManagerA = new ResourceManager("cn.qssq666.Properti ...
- 《Dotnet9》系列-开源C# WPF控件库3《HandyControl》强力推荐
大家好,我是Dotnet9小编,一个从事dotnet开发8年+的程序员.我最近开始写dotnet分享文章,希望能让更多人看到dotnet的发展,了解更多dotnet技术,帮助dotnet程序员应用do ...
- 如何封装属于自己的WPF控件库
在网上找了一下,发现这方面的资料并不多.做传统桌面的本来就不多了吧,更别说WPF了.我可能也要另寻出路了,不过我还是觉得做桌面挺有意思的. 言归正传 首先,新建一个WPF自定义控件库项目 这里我们封 ...
- 浅尝辄止——使用ActiveX装载WPF控件
1 引言 使用VC编写的容器类编辑器,很多都可以挂接ActiveX控件,因为基于COM的ActiveX控件不仅封装性不错,还可以显示一些不错的界面图元. 但是随着技术不断的进步,已被抛弃的Active ...
- 通过WinForm控件创建的WPF控件无法输入的问题
今天把写的一个WPF程序发布到别的机器上执行,发现一个比较奇怪的问题:在那个机器上用英文输入法无法输入数字,非要切换到中文输入法才行:但在我的机器上却是好好的. 最开始以为是输入法的问题,弄了好一阵子 ...
- WPF控件--利用Winform库中的NotifyIcon实现托盘小程序
WPF控件--NotifyIcon 运行界面如下所示: 图1 图2 代码很少,如下所示 ...
- WPF控件模板
引言:在进行WPF项目开发过程中,由于项目的需要,经常要对某个控件进行特殊的设定,其中就牵涉到模板的相关方面的内容.本文也是在自己进行项目开发过程中遇到控件模板设定时集中搜集资料后整理出来的,以供在以 ...
随机推荐
- TextFX Notepad++
textFx Notepad++ - 国内版 Bing https://cn.bing.com/search?FORM=U227DF&PC=U227&q=textFx+Notepad% ...
- 钉钉通知机器人与SpringBoot的集成
Spring Boot Admin 集成自定义监控告警(2.0.1版本)------钉钉机器人 - yuancao24的博客 - CSDN博客https://blog.csdn.net/yuancao ...
- github资源汇总
github免费的编程中文书籍索引 机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 1) Python 资源大全中文版
- [原]DOM、DEM、landcover,从tms服务发布格式转arcgis、google服务发布格式
原作:南水之源 先看看tms和google服务器发布数据的数据排列:(goole地图与arcgis一样) 我现在手上有tms发布的数据,dom,dem等,现在要用arcgis server来发布这些数 ...
- 服务器端实时推送技术之SseEmitter的用法
这是SpringMVC提供的一种技术,可以实现服务端向客户端实时推送数据.用法非常简单,只需要在Controller提供一个接口,创建并返回SseEmitter对象,发送数据可以在另一个接口调用其se ...
- Jmeter之__CSVRead随机读取变量
背景:当你需要从一些数据中随机的取值用的话可以用这个函数来实现__CSVRead __CSVRead函数用于对脚本进行参数化,当脚本中不同变量需要不同参数值时,可以考虑__CSVRead函数. _CS ...
- pytorch如何先初始化变量,然后再赋值
下面是定义初始化 #初始化输入的张量 - torch.empty是返回一个包含未初始化数据的张量 self.input = torch.empty(size=(self.opt.batchsize, ...
- Java PDF转换成图片并输出给前台展示
首先需要导入所需工具类 <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>fo ...
- 安装mysql报错:Can't find messagefile '/usr/share/mysql/english/errmsg.sys'和/usr/bin/mysqladmin: error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or
使用yum安装mysql服务端: [root@centos ~]# yum -y install mysql-server Loaded plugins: fastestmirror, securit ...
- 数据分析入门——pandas之Series
一.介绍 Pandas是一个开源的,BSD许可的库(基于numpy),为Python编程语言提供高性能,易于使用的数据结构和数据分析工具. 官方中文文档:https://www.pypandas.cn ...