ControlTemplate for existing controls in WPF

后端 未结 8 1887
野的像风
野的像风 2020-11-22 12:35

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.

8条回答
  •  醉酒成梦
    2020-11-22 12:52

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

提交回复
热议问题