WPF 中现有控件的 ControlTemplate

发布于 2024-08-07 19:02:33 字数 86 浏览 2 评论 0原文

如何以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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

只有影子陪我不离不弃 2024-08-14 19:02:33

查看StyleSnooper

在此处输入图像描述

它将转储标准样式(因此也有模板)用于内置控件。您还可以加载包含 WPF 控件的特定 DLL 并查看这些控件的默认样式。

Check out StyleSnooper:

enter image description here

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.

情深如许 2024-08-14 19:02:33

样式和模板示例位于 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.

请恋爱 2024-08-14 19:02:33

如果您有 Expression Blend,您可以:

  1. 将控件拖到设计图面上
  2. 右键单击控件并选择编辑模板 ->编辑副本

当您执行此操作时,Blend 将从控件中提取基本模板,并在文档/应用程序中将其显式声明为资源,然后您可以根据自己的喜好进行编辑。您可以对任何控件执行此操作。

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.

追星践月 2024-08-14 19:02:33

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.

瀟灑尐姊 2024-08-14 19:02:33

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:

Right-click on the control > Edit Template> Edit Current OR Edit a Copy

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.

一抹微笑 2024-08-14 19:02:33

您可以使用 ShowMeTheTemplate 等工具

You can use a tool like ShowMeTheTemplate

野心澎湃 2024-08-14 19:02:33

使用 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

温柔嚣张 2024-08-14 19:02:33

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文