WPF 中现有控件的 ControlTemplate
如何以XAML格式(可视化树)获取WPF中现有控件的ControlTemplate? 这是为了帮助在现有模板的帮助下创建新的 ControlTemplate。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
查看StyleSnooper:
它将转储标准样式(因此也有模板)用于内置控件。您还可以加载包含 WPF 控件的特定 DLL 并查看这些控件的默认样式。
Check out StyleSnooper:
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.
样式和模板示例位于 MSDN 如需下载,请参阅
默认WPF 主题
链接。不过,您也可以使用
BasedOn
属性扩展现有样式,而无需重新定义所有内容。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.如果您有 Expression Blend,您可以:
当您执行此操作时,Blend 将从控件中提取基本模板,并在文档/应用程序中将其显式声明为资源,然后您可以根据自己的喜好进行编辑。您可以对任何控件执行此操作。
If you have Expression Blend you can:
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.
Matthew MacDonald 所著的《Pro WPF in C# 2008》一书在第 15 章中包含了一个控件模板浏览器。我相信您可以简单地从 Apress 网站下载示例代码。
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.
2020 更新:
WPF 组件的所有样式和模板现已移动此处。
但是,在创建新组件之前,只需检查 这个看看你是否有一个好的替代方案(也许改变样式可以工作)。
注意:通常,它有一堆 DynamicResource 绑定,您可能需要将其替换为您的绑定并将其设为静态。如果您不想做太多手动工作,您可以考虑使用下面的第二种解决方案。
第二个也是较短的解决方案可能是使用 Microsoft Blend 通过以下步骤提取模板:
但是要小心这个,因为它并不总是导出整个模板,
但它的必要部分。
只需考虑将此模板与官方模板(上面提到的)进行比较,以确保一切正常。
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.
The second and shorter solution might be using Microsoft Blend to extract the template with the following steps:
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.
您可以使用 ShowMeTheTemplate 等工具
You can use a tool like ShowMeTheTemplate
使用 Microsoft Blend 来实现:
将整个 XAML 代码粘贴到此工具的文件中,然后右键单击要感知其视觉树的控件:
选择以下选项:
编辑模板即可
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
XamlWriter 类为您提供了此功能。如果
controlName
是控件的名称然后使用下面的代码片段,您可以在
stringBuilder
对象内获取控件模板的 Xaml。我猜答案中提到的工具利用了这个类。
The XamlWriter class provides you with this functionality. If
controlName
is the Name of a Controlthen 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.