- private 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;
- }
复制代码
该函数的好处是在无法有效设置控件样式的时候,可以拿到原生的样式并进行适当修改,非常爽! |