博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF标准控件模板查看程序(文件里面)
阅读量:6034 次
发布时间:2019-06-20

本文共 4400 字,大约阅读时间需要 14 分钟。

xaml

View Code

cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.Reflection;using System.Xml;using System.Windows.Markup;namespace ControlTemplateBrowser{    ///     /// Interaction logic for MainWindow.xaml    ///     public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);        }        void MainWindow_Loaded(object sender, RoutedEventArgs e)        {            Type controlType=typeof(Control);            List
derivedTypes = new List
(); //Search all the types in the assembly where the control class is defined Assembly assembly = Assembly.GetAssembly(typeof(Control)); foreach (Type type in assembly.GetTypes()) { //only add a type of the list if it's a control, a concrete class, //and public if (type.IsSubclassOf(controlType)&&!type.IsAbstract&&type.IsPublic) { derivedTypes.Add(type); } } //sort the types. the custom typeComparer class orders types //alphabetically by type name derivedTypes.Sort(new TypeComparer()); lstTypes.ItemsSource = derivedTypes; } private void lstTypes_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { //get the selected Type type = (Type)lstTypes.SelectedItem; //Instantiate the type ConstructorInfo info = type.GetConstructor(System.Type.EmptyTypes); Control control = (Control)info.Invoke(null); //Window win = control as Window; //if (win != null) //{ // // Create the window (but keep it minimized). // win.WindowState = System.Windows.WindowState.Minimized; // win.ShowInTaskbar = false; // win.Show(); //} //else //{ // Add it to the grid (but keep it hidden). control.Visibility = Visibility.Collapsed; grid.Children.Add(control); //} // Get the template. ControlTemplate template = control.Template; // Get the XAML for the template. XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; StringBuilder sb = new StringBuilder(); XmlWriter writer = XmlWriter.Create(sb, settings); XamlWriter.Save(template, writer); // Display the template. txtTemplate.Text = sb.ToString(); // Remove the control from the grid. //if (win != null) //{ // win.Close(); //} //else //{
grid.Children.Remove(control); //} } catch (Exception err) { txtTemplate.Text = "<<>Error generating template:" + err.Message+ ">"; } } }}
View Code

TypeComparer

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections;namespace ControlTemplateBrowser{    public class TypeComparer : IComparer
{ public int Compare(Type x, Type y) { if (x == null || y == null) throw new ArgumentException("参数不能为空"); if (x.Name.CompareTo(y.Name) != 0) { return x.Name.CompareTo(y.Name); } else { return 0; } } }}
View Code

 

 

转载于:https://www.cnblogs.com/FaDeKongJian/p/3175958.html

你可能感兴趣的文章
vim文本编辑器详解
查看>>
学习vue中遇到的报错,特此记录下来
查看>>
CentOS7 编译安装 Mariadb
查看>>
32位系统和64位系统的选择
查看>>
01配置管理过程指南
查看>>
jstl格式化时间
查看>>
一则关于运算符的小例
查看>>
centos7 ambari2.6.1.5+hdp2.6.4.0 大数据集群安装部署
查看>>
cronexpression 详解
查看>>
一周小程序学习 第1天
查看>>
小孩的linux
查看>>
SpringMVC、MyBatis声明式事务管理
查看>>
开发者详解:端游及手游服务端的常用架构
查看>>
JavaScript History对象
查看>>
在 Windows 下安装 Oracle 11g XE (Express Edition)
查看>>
ListView优化
查看>>
【原创】 PostgreSQL 实现MySQL 的auto_increment 字段
查看>>
vs2015添加vc助手
查看>>
检测点1.1
查看>>
android--------阿里 AndFix 热修复
查看>>