English 中文(简体)
世界森林论坛现有控制措施的对照表
原标题:ControlTemplate for existing controls in WPF
  • 时间:2009-10-13 10:03:50
  •  标签:

How to get existing control s ControlTemplate in WPF in XAML format (visual tree)? This is to help to create new ControlTemplate with the help of existing template.

最佳回答
问题回答

由Matthew MacDonald撰写的《2008年C#》一书载有第15章中的“控制模板浏览器”。 我认为,你可以简单地从Apress网站下载样本代码。

<2020 Update:

All the styles and templates of WPF components are now moved here.
But, before creating a new component, just check this to see if you have a good alternative to that (maybe changing the style can work).
Note: Usually, it has a bunch of DynamicResource bindings that you might want to replace with yours and make static. If you don t want to do much manual work, you might consider using the second solution below.

第二种和较短的解决办法可能使用Microsoft Blend至extract 。 该模板包括以下步骤:

Right-click on the control > Edit Template>Edit Current OR Edit a pidt a

But be careful with this one, as it doesn t always export the whole template,
but the necessary part of it.
Just consider comparing this template with the official one (mentioned above) to make sure everything is fine.

http://www.sellsbrothers.com/tools/#ShowMeTheTemplate”rel=“nofollow noreferer” 缩略语

Use Microsoft Blend for it: Paste your entire XAML code in a file in this tool and right click the control whose visual tree you want to perceive:

Select the option: Edit template and there you go

The XamlWriter class provides you with this functionality. If controlName is the Name of a Control then using the below snippet you get the Xaml of the Control s Template inside the stringBuilder object. I guess tools mentioned in the answers utilize this class.

var stringBuilder = new StringBuilder();
var xmlSettings = new XmlWriterSettings
{
  Indent = true
};

using (var xmlWriter = XmlWriter.Create(stringBuilder, xmlSettings))
{
  XamlWriter.Save(controlName.Template, xmlWriter);
}




相关问题
热门标签