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. 错误描述:请求“System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”类型的权限已失败

    错误描述:请求“System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, Pu ...

  2. Java中的try,catch(Exception e),finally及return执行顺序

    结论: ①就算之前return,finally也会执行 ②finally的计算结果不影响之前的return值 ③finally的return值一定是最后的返回结果,因此将return放入finally ...

  3. UrlEncoder url编码

    public static string PercentEncode(string s)        {            var bytes = Encoding.UTF8.GetBytes( ...

  4. Match:Cyclic Nacklace(KMP的next数组的高级应用)(HDU 3746)

    串珠子 题目大意:给定一个字串,要你找到如果要使之成为循环串,在末尾需要的最小的字数(只能添加字符,不能删减字符) 首先联动一下之前做过的动态规划问题POJ 3280,当然了3280这一题是用的LD, ...

  5. 【linux】学习5

    鸟哥那本书第11章的内容 管理整个计算机硬件的是操作系统的内核(kernel),内核是需要保护的,我们一般用户只能通过shell来跟内核通信.Shell是用户操作系统的接口 cat  /etc/pas ...

  6. POJ 2965 The Pilots Brothers' refrigerator

    题目链接 题意:一个冰箱上有4*4共16个开关,改变任意一个开关的状态(即开变成关,关变成开)时,此开关的同一行.同一列所有的开关都会自动改变状态.要想打开冰箱,要所有开关全部打开才行. 输入:一个4 ...

  7. php数据访问基础

    建一个连接(造一个连接对象) $db = new MySqli("IP地址或者域名,若果是本地则用localhost","用户名","数据库密码&qu ...

  8. 数独检测器:帝国理工C++作业

    #include <fstream> #include <vector> #include <iostream> #include <string> u ...

  9. UITableView的scrollToRowAtIndexPath:atScrollPosition:animated的崩溃

      UITableView的scrollToRowAtIndexPath:atScrollPosition:animated的崩溃 [摘要:reason: '-[UITableView _conten ...

  10. 环信SDK集成

    利用环信SDK可以实现即时通讯,但在集成的过程中碰到了不少的坑. 注意 选择项目路径,这里以最新版环信demo为例 注意:环信的ChatDemoUI这个demo里边因为研发的同事为了照顾老版本的And ...