简体   繁体   中英

Where does the look of wpf come from?

I just read the book on WPF from Thomas Claudius Huber . He stated, that all WPF controls are "lookless". They just get their look (and visual tree) from their ControlTemplate . That raises one question: Where does the look of WPF come from?

I mean: the Button has a ControlTemplate with some Border s and a ContentPresenter . Where do these two ( Border and ContentPresenter ) get their look from?

I already googled and found, that Border is a Decorator and sets its look in the OnRender -Method.

Is that the bottom line? Do all other elements which don't have a ControlTemplate define their look in the OnRender -Method?

Short answer: Yes . All visual elements that are not Controls and have a "look", define said look in their UIElement.OnRender method override.

Long answer: Controls don't use the OnRender method to define their looks. Instead, their "looks" are defined in Styles and Templates. When no Style or Template is defined explicitly in an application, WPF Controls simply use their default Styles and Templates from the current system Theme (for more info on Themes, check this MSDN article ).

Let's just say that the Framework has its own Resource Dictionaries with default Styles for all built-in controls. For instance, here is the default ControlTemplate of the ComboBox: ComboBox Styles and Templates

That being said, there are several visual components that have their looks defined through code, usually through the OnRender override. They're not Controls; they're Decorators , Shapes and things like that. Things that do have a "look": Border, Rectangle, etc. But in the end, all Controls have look thanks to these elements because all ControlTemplates are made up of either these elements, or other Controls.

TextBlock , as Run , FlowDocument and other similar elements, are special elements created specifically for text renderization. They fall into a similar category than Shapes or Decorators, except they specialize on text rather than graphics. TextBlock, for instance, is not a Control, and defines its look on its OnRender method. Label, on the other hand, IS a Control; but if you check its Template, you'll see it ends up using a TextBlock to show the text.

There are other elements (like ContentPresenter , ItemsPresenter ) that have no look whatsoever, not implicit, not by default, not by Styles or Templates. These are logic elements, that define the structure of the view. ContentPresenter, for instance, grabs the Content and ContentTemplate properties of a ContentControl, and makes sure that said Template is correctly rendered and bound to said data, so to speak. But they have no visual representation of their own.

Oh, and I almost forgot about Panels . Panels are not Controls, either, and they do have a look of their own. But similarly to Presenters, they're also logic elements that define how the other visual elements are visualized. More specifically, their layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM