ControlTemplate for existing controls in WPF

青春壹個敷衍的年華 提交于 2019-11-26 03:16:00

问题


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.


回答1:


The styles along with template examples are up on MSDN for download, see the Default WPF Themes link.

However you can also extend the existing style without redefining everything by using the BasedOn attribute.




回答2:


Check out StyleSnooper:


(source: intuidev.com)

It will dump out the standard styles (and therefore templates too) for the built in controls. You can also load in a specific DLL that contains WPF controls and view the default styles for those too.




回答3:


If you have Expression Blend you can:

  1. Drag the control onto the design surface
  2. Right click the control and choose Edit Template -> Edit Copy

When you do this, Blend will extract the base template from the control and explicitly declare it within document/application as a resource which you can then edit to your liking. You can do this for any control.




回答4:


The book "Pro WPF in C# 2008", by Matthew MacDonald, includes a Control Template browser in Chapter 15. I believe you can simply download the sample code from the Apress web site.




回答5:


You can use a tool like ShowMeTheTemplate




回答6:


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




回答7:


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


来源:https://stackoverflow.com/questions/1559261/controltemplate-for-existing-controls-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!