WPF标准控件模板查看程序(文件里面)
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标准控件模板查看程序(文件里面)的更多相关文章
- WPF默认控件模板的获取和资源词典的使用
一.获取默认的控件模板 WPF修改控件模板是修改外观最方便的方式,但是会出现不知道原来的控件的模板长什么样,或者想用来参考的,下面分享一下获取某控件默认控件模板的方式(已Button为例): 1.创建 ...
- WPF 获取控件模板中的控件
DG是控件名称public T GetVisualChild<T>(DependencyObject parent, Func<T, bool> predicate) wher ...
- 解决wpf popup控件遮挡其他程序的问题
public class PopupNonTopmost : Popup { public static DependencyProperty TopmostProperty = Window.Top ...
- WPF 寻找控件模板中的元素
<Window x:Class="Wpf180706.Window10" xmlns="http://schemas.microsoft.com/wi ...
- WPF Button控件模板
<Window x:Class="ControlTemplateDemo.MainWindow" xmlns="http://schemas.m ...
- 【WPF学习】第五十九章 理解控件模板
最近工作比较忙,未能及时更新内容,敬请了解!!! 对于可视化树的分析引出了几个有趣问题.例如,控件如何从逻辑树表示扩张成可视化树表示? 每个控件都有一个内置的方法,用于确定如何渲染控件(作为一组更基础 ...
- WPF控件模板
引言:在进行WPF项目开发过程中,由于项目的需要,经常要对某个控件进行特殊的设定,其中就牵涉到模板的相关方面的内容.本文也是在自己进行项目开发过程中遇到控件模板设定时集中搜集资料后整理出来的,以供在以 ...
- WPF数据模板和控件模板
WPF中有控件模板和数据模板,控件模板可以让我们自定义控件的外观,而数据模板定义了数据的显示方式,也就是数据对象的可视结构,但是这里有一个问题需要考虑,数据是如何显示出来的?虽然数据模板定义了数 ...
- 【WPF学习】第六十章 创建控件模板
经过数十天的忙碌,今天终于有时间写博客. 前面一章通过介绍有关模板工作方式相关的内容,同时介绍了FrameWorkElement下所有控件的模板.接下来将介绍如何构建一个简单的自定义按钮,并在该过程中 ...
随机推荐
- DIV UL LI
<style type="text/css"> .productDetailTabNav{ width:960px;} .productDetailTabNav ul{ ...
- C#之Windows消息处理
public enum WindowsMessage:int { /// <summary> /// /// </summary> WM_NULL = 0x0000, /// ...
- POJ 2352
---恢复内容开始--- http://poj.org/problem?id=2352 这是一个树状数组的题目,也是我第一次接触这类的题目,也正是因为之前的一道题,TLE了,超时太严重了,我看disc ...
- 6.nodejs权威指南--进程
1. 进程 var net = require('net'); var cluster = require('cluster'); cluster.setupMaster({ exec:'child. ...
- Java for LeetCode 227 Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- eclipse添加字体
1.打开window—>Preferences—>General—>Appeatance—>Colors and Fonts—>Text Font—>Edit 2. ...
- HDU 4940 Destroy Transportation system(无源汇有上下界最大流)
看不懂题解以及别人说的集合最多只有一个点..... 然后试了下题解的方法http://blog.sina.com.cn/s/blog_6bddecdc0102uzka.html 首先是无源汇有上下界最 ...
- 【编程之美】2.5 寻找最大的k个数
有若干个互不相等的无序的数,怎么选出其中最大的k个数. 我自己的方案:因为学过找第k大数的O(N)算法,所以第一反应就是找第K大的数.然后把所有大于等于第k大的数取出来. 写这个知道算法的代码都花了2 ...
- 【XLL 文档翻译】【第3部分】必要的和有用的 C API XLM 函数
本节中将介绍几个对于 DLL 和 XLL 开发人员来说十分重要的回调函数,xlfRegister 函数是可用于注册函数,使得 Excel 可以直接访问 DLL 和 XLl 中的函数. xlfUnreg ...
- 118. Pascal's Triangle
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...