您的位置:首页 > 其它

获取WPF所有控件的模板内容

2011-11-21 16:26 495 查看
本篇文章介绍的内容对于使用Visual Studio开发WPF界面的开发者来说,意义很重大,当然,如果你也在使用Expression Blend4来进行开发时,意义就不是非常明显了.

1. 首先创建XMAL文件

<Window x:Class="ControlTemplateBrowser.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ControlTemplateBrowser" Height="544" Width="713" Loaded="Window_Loaded">
<Grid Margin="10" Name="grid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="3*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox DisplayMemberPath="Name" Name="lstTypes" SelectionChanged="lstTypes_SelectionChanged"></ListBox>
<TextBox Grid.Column="1" Name="txtTemplate" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible" FontFamily="Consolas"></TextBox>
</Grid>
</Window>
2. 编写程序代码

using System;
using System.Collections.Generic;
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.Shapes;
using System.Reflection;
using System.Xml;
using System.Windows.Markup;

namespace ControlTemplateBrowser
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>

public partial class Window1 : System.Windows.Window
{

public Window1()
{
InitializeComponent();
}

private void Window_Loaded(object sender, EventArgs 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 by type name.
derivedTypes.Sort(new TypeComparer());

// Show the list of types.
lstTypes.ItemsSource = derivedTypes;
}

private void lstTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
// Get the selected type.
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 + ">>";
}
}
}

public class TypeComparer : IComparer<Type>
{
public int Compare(Type x, Type y)
{
return x.Name.CompareTo(y.Name);
}
}
}
编译程序,大功告成拉!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息