WPF默认模板调用方法简介

开发 开发工具
我们在文章中用一个代码示例来为大家详细介绍了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. }  

 

责任编辑:曹凯 来源: IT168
相关推荐

2009-12-23 18:06:25

WPF模板

2009-12-14 16:44:56

Ruby调用DLL

2010-01-06 17:06:05

Json格式

2009-12-24 14:08:25

WPF数据模板

2010-02-01 10:04:25

Python模板函数

2009-12-28 10:40:13

WPF调用Winfor

2009-12-28 09:24:58

WPF线程

2009-12-28 15:08:12

WPF字体

2009-12-25 10:05:06

WPF资源

2009-12-24 14:18:57

WPF类型转换

2009-12-25 14:35:34

WPF布局

2009-12-28 15:48:14

WPF窗口颜色

2009-12-24 16:36:06

WPF InkCanv

2010-04-07 15:47:32

Oracle 11g

2009-12-28 17:33:19

WPF视频音频

2009-12-28 16:00:36

WPF样式继承

2009-12-24 18:09:23

WPF开发环境

2009-12-24 17:57:53

WPF页面跳转

2009-08-19 14:35:12

C++和C#相互调用C

2009-12-28 11:14:29

WPF显示文本
点赞
收藏

51CTO技术栈公众号