WPF默认模板调用方法简介

WPF中具有一些模板,在学习的过程中我们需要熟练的掌握这些模板的应用。在这里我们先来了解一下WPF默认模板调用的具体方法。#t#

***的模板示例:我们知道每个控件都有自己默认的模板,这是MS编写的,如果我们能够得到这些模板的XAML代码,那么它将是学习模板的***的示例,

要想获得某个控件ctrl的默认模板,请调用以下方法:

 

 
 
 
  1. string GetTemplateXamlCode
    (Control ctrl) {  
  2. FrameworkTemplate template
     = ctrl.Template;   
  3. string xaml = "";   
  4. if (template != null) {   
  5. XmlWriterSettings settings =
     
    new XmlWriterSettings();  
  6. settings.Indent = true;   
  7. settings.IndentChars = 
    new string(' ', 4);  
  8. settings.NewLineOnAttributes = true;   
  9. StringBuilder strbuild = 
    new StringBuilder();   
  10. XmlWriter xmlwrite = 
    XmlWriter.Create(strbuild, 
    settings); 
  11. try { XamlWriter.Save(template,
     xmlwrite);  
  12. xaml = strbuild.ToString();   
  13. }   
  14. catch (Exception exc)   
  15. xaml = exc.Message;   
  16. }   
  17. }   
  18. else {   
  19. xaml = "no template";   
  20. }   
  21. return xaml;   
  22. }  

 

THE END