.net WPF 中现有控件的 ControlTemplate
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1559261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ControlTemplate for existing controls in WPF
提问by
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.
如何以 XAML 格式(可视化树)获取 WPF 中现有控件的 ControlTemplate?这是为了帮助在现有模板的帮助下创建新的 ControlTemplate。
采纳答案by Akash Kava
The styles along with template examples are up on MSDNfor download, see the Default WPF Themeslink.
样式和模板示例可在 MSDN上下载,请参阅Default WPF Themes链接。
However you can also extend the existing style without redefining everything by using the BasedOnattribute.
但是,您也可以使用该BasedOn属性扩展现有样式而无需重新定义所有内容。
回答by Drew Noakes
Check out StyleSnooper:
查看StyleSnooper:

(source: intuidev.com)

(来源: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.
它将转储内置控件的标准样式(以及模板)。您还可以加载包含 WPF 控件的特定 DLL 并查看这些控件的默认样式。
回答by Drew Marsh
If you have Expression Blendyou can:
如果您有Expression Blend,您可以:
- Drag the control onto the design surface
- 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.
执行此操作时,Blend 将从控件中提取基本模板,并在文档/应用程序中将其显式声明为资源,然后您可以根据自己的喜好对其进行编辑。您可以为任何控件执行此操作。
回答by David Veeneman
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.
Matthew MacDonald 所著的“Pro WPF in C# 2008”一书在第 15 章中包含一个控制模板浏览器。我相信您只需从 Apress 网站下载示例代码即可。
回答by Arushi Agrawal
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:
使用 Microsoft Blend:将整个 XAML 代码粘贴到此工具中的文件中,然后右键单击要感知其可视化树的控件:
Select the option: Edit template and there you go
选择选项:编辑模板,然后就可以了
回答by Thomas Levesque
You can use a tool like ShowMeTheTemplate
您可以使用ShowMeTheTemplate 之类的工具
回答by arpl
The XamlWriter class provides you with this functionality. If controlNameis the Name of a Control
then using the below snippet you get the Xaml of the Control's Template inside the stringBuilderobject.
I guess tools mentioned in the answers utilize this class.
XamlWriter 类为您提供了此功能。如果controlName是控件的名称,则使用下面的代码段,您将获得stringBuilder对象内控件模板的 Xaml 。我猜答案中提到的工具使用了这个类。
var stringBuilder = new StringBuilder();
var xmlSettings = new XmlWriterSettings
{
Indent = true
};
using (var xmlWriter = XmlWriter.Create(stringBuilder, xmlSettings))
{
XamlWriter.Save(controlName.Template, xmlWriter);
}
回答by Just Shadow
2020 Update:
2020 更新:
All the styles and templates of WPF components are now moved here.
But, before creating a new component, just check thisto 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.
WPF 组件的所有样式和模板现在都移到了这里。
但是,在创建新组件之前,只需检查一下,看看您是否有更好的替代方案(也许更改样式可以工作)。
注意:通常,它有一堆 DynamicResource 绑定,您可能希望将其替换为您的绑定并使其成为静态。如果您不想做太多的手动工作,您可以考虑使用下面的第二种解决方案。
The second and shorter solution might be using Microsoft Blend to extractthe template with the following steps:
第二个更短的解决方案可能是使用 Microsoft Blend通过以下步骤提取模板:
Right-click on the control >
Edit Template>Edit CurrentOREdit a Copy
右键单击控件 >
Edit Template>Edit Current或Edit a Copy
But be carefulwith 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.
但是要小心这个,因为它并不总是导出整个模板,
而是它的必要部分。
只需考虑将此模板与官方模板(如上所述)进行比较以确保一切正常。

