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

 

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  
	 {  
	     ///   
	     /// Interaction logic for Window1.xaml   
	     ///    
	   
	     public partial class Window1 : System.Windows.Window  
	     {  
	   
	         public Window1()  
	         {  
	             InitializeComponent();  
	         }  
	   
	         private void Window_Loaded(object sender, EventArgs e)  
	         {                      
	             Type controlType = typeof(Control);  
	             List derivedTypes = new List();  
	   
	             // 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  
	     {  
	        public int Compare(Type x, Type y)  
	        {  
	           return x.Name.CompareTo(y.Name);  
	        }  
	    }  
	}  

 

 

你可能感兴趣的:(WPF)