简体   繁体   English

wpf的外观来自哪里?

[英]Where does the look of wpf come from?

I just read the book on WPF from Thomas Claudius Huber . 我刚读过Thomas Claudius Huber关于WPF的书。 He stated, that all WPF controls are "lookless". 他表示,所有WPF控件都是“不露面”的。 They just get their look (and visual tree) from their ControlTemplate . 他们只是从他们的ControlTemplate获取他们的外观(和可视化树)。 That raises one question: Where does the look of WPF come from? 这提出了一个问题: WPF的外观来自哪里?

I mean: the Button has a ControlTemplate with some Border s and a ContentPresenter . 我的意思是: Button有一个带有一些Border s和一个ContentPresenterControlTemplate Where do these two ( Border and ContentPresenter ) get their look from? 这两个( BorderContentPresenter )从哪里看?

I already googled and found, that Border is a Decorator and sets its look in the OnRender -Method. 我已经用Google搜索并发现, Border是一个Decorator并在OnRender -Method中设置它的外观。

Is that the bottom line? 那是底线吗? Do all other elements which don't have a ControlTemplate define their look in the OnRender -Method? 没有ControlTemplate的所有其他元素是否在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. 所有不是控件且具有“外观”的视觉元素,在其UIElement.OnRender方法覆盖中定义所述外观。

Long answer: Controls don't use the OnRender method to define their looks. 答案很长: 控件不使用OnRender方法来定义它们的外观。 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 ). 如果在应用程序中没有显式定义样式或模板,WPF控件只需使用当前系统主题中的默认样式和模板(有关主题的更多信息,请查看此MSDN文章 )。

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 例如,这是ComboBox的默认ControlTemplate: ComboBox样式和模板

That being said, there are several visual components that have their looks defined through code, usually through the OnRender override. 话虽这么说,有几个可视组件通过代码定义其外观,通常通过OnRender覆盖。 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. 具有“外观”的东西:边框,矩形等。但最后,由于所有ControlTemplates都由这些元素或其他控件组成,因此所有控件都具有这些元素。

TextBlock , as Run , FlowDocument and other similar elements, are special elements created specifically for text renderization. TextBlock ,作为RunFlowDocument和其他类似元素,是专门为文本渲染创建的特殊元素。 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. 例如,TextBlock不是Control,并在其OnRender方法上定义其外观。 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. 但是如果你检查它的模板,你会看到它最终使用TextBlock来显示文本。

There are other elements (like ContentPresenter , ItemsPresenter ) that have no look whatsoever, not implicit, not by default, not by Styles or Templates. 还有其他元素(如ContentPresenterItemsPresenter )没有任何外观,不是隐式的,不是默认情况下,不是样式或模板。 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. 例如,ContentPresenter获取ContentControl的ContentContentTemplate属性,并确保所述模板正确呈现并绑定到所述数据,可以这么说。 But they have no visual representation of their own. 但他们没有自己的视觉表现。

Oh, and I almost forgot about Panels . 哦,我几乎忘记了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. 但与Presenters类似,它们也是定义其他视觉元素如何可视化的逻辑元素。 More specifically, their layout. 更具体地说,他们的布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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