效果图:
<Window x:Class="WpfApplication1.Window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window3" Height="553.847" Width="774.231">
<DockPanel LastChildFill="True">
<Border Height="50" DockPanel.Dock="Top" BorderBrush="Blue">
<StackPanel Orientation="Horizontal">
<Button x:Name="btnShowLogicalTree" Content="编程方式查看 逻辑树"
Margin="4" BorderBrush="Blue" Height="40" Click="btnShowLogicalTree_Click"/>
<Button x:Name="btnShowVisualTree" Content="编程方式查看 可视化树"
BorderBrush="Blue" Height="40" Click="btnShowVisualTree_Click"/>
</StackPanel>
</Border>
<TextBox x:Name="txtDisplayArea" Margin="10" Background="AliceBlue" IsReadOnly="True" BorderBrush="Red"
HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Width="400"
/>
<Border DockPanel.Dock="Right" Margin="10" BorderBrush="DarkGreen" BorderThickness="4" >
<StackPanel>
<Label Content="Enter Full Name of WPF Control(查看控件默认模板)" Width="340" FontWeight="demiBold"/>
<TextBox x:Name="txtFullName" Width="340" BorderBrush="Green" Background="BlanchedAlmond" Height="22"
Text="System.Windows.Controls.Button"/>
<Button x:Name="btnTemplate" Content="See Template" BorderBrush="Green" Height="40" Width="100" Margin="5"
Click="btnTemplate_Click" HorizontalAlignment="Left"/>
<Border BorderBrush="DarkGreen" BorderThickness="2" Height="260" Width="301" Margin="10" Background="LightGreen">
<StackPanel x:Name="stackTemplatePanel"/>
</Border>
</StackPanel>
</Border>
</DockPanel>
</Window>
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.Shapes;
using System.Reflection;
using System.Xml;
using System.Windows.Markup;
namespace WpfApplication1
{
/// <summary>
/// Window3.xaml 的交互逻辑
/// </summary>
public partial class Window3 : Window
{
private string dataToShow = string.Empty;
private Control ctrlToExamine = null;
public Window3()
{
InitializeComponent();
}
private void btnShowLogicalTree_Click(object sender, RoutedEventArgs e)
{
dataToShow = "";
BuildLogicalTree(0, this);
this.txtDisplayArea.Text = dataToShow;
}
//编程方式查看 逻辑树
void BuildLogicalTree(int depth,object obj)
{
dataToShow += new string(' ', depth) + obj.GetType().Name + "\n";
if (!(obj is DependencyObject))
return;
//LogicalTreeHelper.GetChildren 获取逻辑树子对象
//obj as DependencyObject 将obj转换成 依赖对象
foreach(object child in LogicalTreeHelper.GetChildren(obj as DependencyObject))
BuildLogicalTree(depth+5,child);
}
private void btnShowVisualTree_Click(object sender, RoutedEventArgs e)
{
dataToShow = "";
BuildVisualTree(0, this);
this.txtDisplayArea.Text = dataToShow;
}
//编程方式查看 可视化树
void BuildVisualTree(int depth, DependencyObject obj)
{
dataToShow += new string(' ', depth) + obj.GetType().Name + "\n";
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj);i++ )
{
BuildVisualTree(depth + 1, VisualTreeHelper.GetChild(obj, i));
}
}
private void btnTemplate_Click(object sender, RoutedEventArgs e)
{
dataToShow = "";
ShowTemplate();
this.txtDisplayArea.Text = dataToShow;
}
//控件模板
private void ShowTemplate()
{
if (ctrlToExamine != null)
stackTemplatePanel.Children.Remove(ctrlToExamine);
try {
Assembly asm = Assembly.Load("PresentationFramework,version=4.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35");
ctrlToExamine = (Control)asm.CreateInstance(txtFullName.Text);
ctrlToExamine.Height = 200;
ctrlToExamine.Width = 200;
ctrlToExamine.Margin = new Thickness(5);
stackTemplatePanel.Children.Add(ctrlToExamine);
//定义xml一些设置,以保持缩进
XmlWriterSettings xmlSetting = new XmlWriterSettings();
xmlSetting.Indent = true;
//创建 StringBuilder 来保存xml
StringBuilder strBuilder = new StringBuilder();
//创建基于设置的 XmlWriter
XmlWriter xWriter = XmlWriter.Create(strBuilder,xmlSetting);
//将基于ControlTemplate的XAML保存到XmlWriter对象
XamlWriter.Save(ctrlToExamine.Template,xWriter);
//文本框显示XAML
dataToShow = strBuilder.ToString();
}
catch(Exception ex) {
dataToShow = ex.Message;
}
}
}
}