WPF中获取系统本身自带的控件模板(XAML)

2017-07-26 21:18:39来源:cnblogs.com作者:博轩客园人点击

分享

       每个控件都有自己默认的模板,这是MS本身就编写好的,如果我们能够得到这些模板的XAML代码,那么它将是学习模板的最好的示例,要想获得某个控件ctrl的默认模板,请调用以下方法:

 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         }

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台