1

MSDN documentation specifies that when defining a custom control, one should define a control contract. From that page, it is so defined:

[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
//...

I cannot find anywhere in the documentation where it specifies how to discover this contract. If I am overriding the control template, how do I know that I've handled all VisualStates, and that I've implemented all Parts?

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 (which may not be available for 3rd party controls)?

Travis
  • 1,044
  • 1
  • 17
  • 36
  • Consulting the documentation or the metatdata of the control is the way to go. You should consider whether you really want to use third-party libraries without decent documentation at all. – mm8 Aug 05 '20 at 14:56
  • As a side has anyone ever seen a contracts for Microsoft WPF controls, I haven't seen any in documentation. I wonder whether this is practiced by anyone? It seems that customization is provided by dependency properties of controls. Maybe I'm wrong, as I am not using third part libraries that much, only MS ones. Does anyone re-template controls completely? – Piotr Golacki Jun 16 '22 at 11:52

1 Answers1

2

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

Yes.

Either you refer to official documentation or you have to find the corresponding attributes using reflection or find the template parts, visual states and properties by looking at the C# code and XAML resources. Even worse, while one should define a contract, not all controls do and in that case reflection does not even help.

However, most commercial libraries like DevExpress or Telerik offer a pretty good documentation for styling and templating. For WPF controls, you can extract styles and control templates via Blend or Visual Studio and consult the reference source in the worst case, if the documentation does not provide enough information. If there is no documentation and reference source for a particular library, it is guess or decompile.

thatguy
  • 21,059
  • 6
  • 30
  • 40
  • I take issue with the implied obviousness that "duh" indicates. Most other language constructs, particularly where contracts are involved, are discoverable via Intellisense, _in some way_. That this particular one does not is surprising to me. – Travis Aug 04 '20 at 22:35
  • @Travis Sorry, "(duh)" should *not* indicate the obviousness, but the unfortunate event that this is still an issue without a convenient solution. I completely agree with you, one would expect that this has to be supported in any way, but you have already researched relevant sources, so credits to you, I cannot add much to that. – thatguy Aug 05 '20 at 06:23