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

本文共 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

你可能感兴趣的文章
【风马一族_物理】维度空间的粒子
查看>>
手把手教你如何把java代码,打包成jar文件以及转换为exe可执行文件
查看>>
Codeforces Round #363 Fix a Tree(树 拓扑排序)
查看>>
hihocoder1455 Rikka with Tree III(bitset 莫队 dfs序)
查看>>
SQL Server 2008中的MERGE(不仅仅是合并)
查看>>
啤酒与饮料算法
查看>>
xxx is not in the sudoers file.This incident will be reported.的解决方法
查看>>
Java实现冒泡排序、折半查找
查看>>
[C++] 引用
查看>>
Drupal7 Module chapter 1 (猪扒7的开发)第一章
查看>>
Django - admin管理工具
查看>>
获取本月天数 和 本月日期
查看>>
js高阶函数map和reduce
查看>>
vue渲染时对象里面的对象的属性提示undefined,但渲染成功
查看>>
贴一份用delphi修改注册表改网卡MAC地址的代码
查看>>
JS获取页面URL信息
查看>>
tomcat
查看>>
WebStorm 快键键
查看>>
Google code android 开源项目 集合
查看>>
tomcat 调优
查看>>