WPF默認模板調用方法簡介
作者:佚名
我們在文章中用一個代碼示例來為大家詳細介紹了WPF默認模板的調用相關技巧,方便大家了解,希望通過這篇文章的介紹能充分掌握這一知識。
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;
- }
責任編輯:曹凱
來源:
IT168