WPF中具有一些模板,在学习的过程中我们需要熟练的掌握这些模板的应用。在这里我们先来了解一下WPF默认模板调用的具体方法。#t#
***的模板示例:我们知道每个控件都有自己默认的模板,这是MS编写的,如果我们能够得到这些模板的XAML代码,那么它将是学习模板的***的示例,
要想获得某个控件ctrl的默认模板,请调用以下方法:
- 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;
- }