当前位置:首页 > 获得WPF控件默认模板ControlTemplate的方法

获得WPF控件默认模板ControlTemplate的方法

点击次数:2563  更新日期:2010-12-20

wpf中的控件都具有默认的模板,要想获得某个控件的默认模板ControlTemplate,请调用下面这个方法:


string GetTemplateXamlCode(Control ctrl)
{
FrameworkTemplate template= ctrl.Template;
string xaml = "";
if (template != null)
{
XmlWriterSettings settings =new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars =new string(\' \', 4);
settings.NewLineOnAttributes = true;
StringBuilder strbuild =new StringBuilder();
XmlWriter xmlwrite =XmlWriter.Create(strbuild,settings);
try
{
XamlWriter.Save(template,xmlwrite);
xaml = strbuild.ToString();
}
catch (Exception exc)
{
xaml = exc.Message;
}
}
else {
xaml = "no template";
}
return xaml;
}