查找 WPF 控件模板部件和状态

Finding WPF Control Template Parts and States

MSDN documentation 指定在定义自定义控件时,应定义一个控件契约。从那个页面,它是这样定义的:

[TemplatePart(Name = "UpButtonElement", Type = typeof(RepeatButton))]
[TemplatePart(Name = "DownButtonElement", Type = typeof(RepeatButton))]
[TemplateVisualState(Name = "Positive", GroupName = "ValueStates")]
//...rest cut for length
public class NumericUpDown : Control
//...

我在文档中找不到指定如何发现此合同的任何地方。如果我覆盖控件模板,我怎么知道我已经处理了所有 VisualStates,并且我已经实现了所有部分?

This answer links to the same documentation, and provides a method for doing this with reflection, but that is not helpful at design time. Am I truly limited to essentially writing a console app that dumps the reflected attributes, or consulting the documentation(第 3 方控件可能不可用)?

Am I truly limited to essentially writing a console app that dumps the reflected attributes, or consulting the documentation [...] ?

是。

要么参考官方文档,要么使用反射找到相应的属性,要么通过查看C#代码和XAML资源找到模板部件、视觉状态和属性。更糟糕的是,虽然 应该 定义契约,但并非所有控件都这样做,在这种情况下反射甚至没有帮助。

但是,大多数商业库,如 DevExpressTelerik 都提供了非常好的样式和模板文档。对于 WPF 控件,如果文档没有提供足够的信息,您可以通过 Blend 或 Visual Studio 提取样式和控件模板,并在最坏的情况下查阅参考资料。如果没有特定库的文档和参考源,那就是猜测或反编译。