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下所有控件的模板.接下来将介绍如何构建一个简单的自定义按钮,并在该过程中 ...
随机推荐
- Calendar类测试
public static void main(String[] args) throws ParseException { // 字符串转换日期格式 // DateFormat fmtDateTim ...
- 如何将ADT项目导入Android studio及常見問題
ADT导出Android studio项目 右键-->ExportAndroid/Generate Gradle build files--> Android studio导入项目 Fil ...
- eclipse添加字体
1.打开window—>Preferences—>General—>Appeatance—>Colors and Fonts—>Text Font—>Edit 2. ...
- SAP打印出库单 新需求
*&---------------------------------------------------------------------* *& Report Z_SD_CKD ...
- 4.kvm克隆虚拟机
virt-clone 作用简介 virt-clone 主要是用来克隆kvm虚拟机,并且通过 Options.General Option.Storage Configuration.Networkin ...
- SQL Server 子查询
这些主要是老师上课讲的一些知识点,自己做了一些整理放在这里~~~ 子查询可以是标量的.多值的或是表值的. 在期待单个值的地方可以使用标量子查询.例如,以下查询返回具有最大员工编号的员工信息: SELE ...
- asp.net 图片下载
string fileName = "123.jpg";//图片名字 string filePath = Server.MapPath(&qu ...
- 阿里云服务器 && 如何window链接到阿里云服务器
现在的时间是:2016年10月11日 1:购买学生机 阿里云手机app上 -> 学生专区 -> 购买: 需要注意的是:如果没有自己需要的系统,比如没有linux操作系统的ecs,那 ...
- IOS管理文件和目录NSFileManager
1.常见的NSFileManager文件方法 -(NSData *)contentsAtPath:path //从一个文件读取数据 -(BOOL)createFileAtPath: path cont ...
- 1.1-java创建包和类的方法
1.new-package-命名方法com打头.中间名称.后台要创建的class 2.创建class- new-class 选择一下主方法 代码示例 编译完保存一下就能输出信息,一直没有保存才 ...