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.
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);
}