Obtain the built-in control template (XAML) and wpfxaml in WPF.
Each control has its own default template, which is compiled by MS itself. If we can get the XAML code of these templates, it will be the best example of learning templates, to obtain the default template of a control ctrl, call the following method:
1 string GetTemplateXamlCode(Control ctrl) 2 { 3 4 FrameworkTemplate template = ctrl.Template; 5 6 string xaml = ""; 7 8 if (template != null) 9 {10 11 XmlWriterSettings settings = new XmlWriterSettings();12 settings.Indent = true;13 settings.IndentChars = new string(' ', 4);14 settings.NewLineOnAttributes = true;15 16 StringBuilder strbuild = new StringBuilder();17 XmlWriter xmlwrite = XmlWriter.Create(strbuild, settings);18 19 try20 {21 XamlWriter.Save(template, xmlwrite);22 xaml = strbuild.ToString();23 }24 catch (Exception exc)25 {26 xaml = exc.Message;27 }28 }29 else30 {31 xaml = "no template";32 }33 34 return xaml;35 }