xaml

<Window x:Class="ControlTemplateBrowser.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="" Width="">
<Grid Name="grid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="7*"/>
</Grid.ColumnDefinitions> <ListBox x:Name="lstTypes" DisplayMemberPath="Name" SelectionChanged="lstTypes_SelectionChanged"/>
<TextBox x:Name="txtTemplate" Grid.Column="" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible" FontFamily="Consolas"/>
</Grid>
</Window>

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
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
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<Type> derivedTypes = new List<Type>(); //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+ ">";
} }
}
}

TypeComparer

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

WPF标准控件模板查看程序(文件里面)的更多相关文章

  1. WPF默认控件模板的获取和资源词典的使用

    一.获取默认的控件模板 WPF修改控件模板是修改外观最方便的方式,但是会出现不知道原来的控件的模板长什么样,或者想用来参考的,下面分享一下获取某控件默认控件模板的方式(已Button为例): 1.创建 ...

  2. WPF 获取控件模板中的控件

    DG是控件名称public T GetVisualChild<T>(DependencyObject parent, Func<T, bool> predicate) wher ...

  3. 解决wpf popup控件遮挡其他程序的问题

    public class PopupNonTopmost : Popup { public static DependencyProperty TopmostProperty = Window.Top ...

  4. WPF 寻找控件模板中的元素

    <Window x:Class="Wpf180706.Window10"        xmlns="http://schemas.microsoft.com/wi ...

  5. WPF Button控件模板

     <Window x:Class="ControlTemplateDemo.MainWindow"        xmlns="http://schemas.m ...

  6. 【WPF学习】第五十九章 理解控件模板

    最近工作比较忙,未能及时更新内容,敬请了解!!! 对于可视化树的分析引出了几个有趣问题.例如,控件如何从逻辑树表示扩张成可视化树表示? 每个控件都有一个内置的方法,用于确定如何渲染控件(作为一组更基础 ...

  7. WPF控件模板

    引言:在进行WPF项目开发过程中,由于项目的需要,经常要对某个控件进行特殊的设定,其中就牵涉到模板的相关方面的内容.本文也是在自己进行项目开发过程中遇到控件模板设定时集中搜集资料后整理出来的,以供在以 ...

  8. WPF数据模板和控件模板

     WPF中有控件模板和数据模板,控件模板可以让我们自定义控件的外观,而数据模板定义了数据的显示方式,也就是数据对象的可视结构,但是这里有一个问题需要考虑,数据是如何显示出来的?虽然数据模板定义了数 ...

  9. 【WPF学习】第六十章 创建控件模板

    经过数十天的忙碌,今天终于有时间写博客. 前面一章通过介绍有关模板工作方式相关的内容,同时介绍了FrameWorkElement下所有控件的模板.接下来将介绍如何构建一个简单的自定义按钮,并在该过程中 ...

随机推荐

  1. C#之系统自带保存属性

    源代码下载链接 程序开发很多时候需要根据运行环境做不通的参数配置,通过写ini之类的文本文件是一种方法,但这种方法也同时会把数据暴露 Winform开发中可以将需要配置的字段属性保存到程序中(其实也是 ...

  2. 自动编译和提交脚本(结合svn和visual studio)

    @echo 更新代码开始----------------- TortoiseProc.exe /command:update /path:"D:\work\mmsanguo_publish_ ...

  3. c++ 文件utf-8格式

    #include <stdio.h> int i = 0; while (i < 20) {    i++;    WriteLog("d:\\log.txt", ...

  4. vSphere Client无法连接到服务器 出现未知错误的解决方法

    VMware ESXi服务器虚拟机在正常使用过程中,有时候会突然出现远程连接不上的问题,那么这个时候使用vSphere Client连接会出现如下错误: 虽然连接不上,但是可以ping通,所以分析有可 ...

  5. FASTREPORT 整理 (mtm)

    DELPHI中用FASTREPORT制作报表  1.加载并存储报表 默认情况下,报表窗体同项目窗体构存储在同一个DFM文件中.多数情况下,无须再操作,因而你就不必采用特殊方法加载报表. 如果你决定在文 ...

  6. WdatePicker组件不显示

    突然发现时间组件不显示了,以为是浏览器的问题.在本地服务器测试了一下发现一切正常. 怀疑是前段时间中毒引起的,用工具比对了一下WdatePicker的文件包,发现My97DatePicker.htm这 ...

  7. hdu 1972.Printer Queue 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1972 题目意思:需要模拟打印机打印.打印机里面有一些 job,每个job被赋予1-9的其中一个值,越大 ...

  8. osgi学习

    Bundle可以被动态地安装.启动.停止和卸载.Bundle是服务(Service)和组件(Component)的载体.在OSGi中,每个Bundle都有自己独立于其他Bundle的ClassLoad ...

  9. C语言中一个替换 strcpy的极好的方法

    在C语言中有个方法:strcpy() 使用时经常容易内存申请不足,或是没有申请内存导致,复制的时候报错,我新写了一个方法,弥补这个缺陷 char *strcpy1(char *strDes, char ...

  10. Linux 通过os进程pid找到端口号

    [root@xxxx conf]# netstat -lutpn|grep tcp /java tcp /java tcp /java tomcat 监听的一些端口