WPF Customer Constrol

What’s control in WPF?

    Control is always some notion of a component that encapsulates some  pp behavior, object model, and display. WPF is no exception is no exception.

Control Principles

• Element composition
    – Previous framework was a lack of consistent flexibility
    – Every place needs a button has to create a new implementation
• Rich content
    – In Win32 world, only RichEdit control can have richly formatted text
• Simple programming model

ContentPresenter ContentPresenter
    ContentPresenter is the one who do the real work

ContentPresenter rules
1. If Content is a UIElement, then parent it
2 If ContentTemplate is set then use that to create a 2. If ContentTemplate is set, then use that to create a UIElement and parent that
3. If ContentTemplateSelector is set, then use that to find
a template and use that to create a UIElement and a template and use that to create a UIElement and parent that
4. If the data type of Content has a data template associated with it, use that to create a UIElement
5. If the data type of Content has a TypeConverter associated with it that can convert to a UIElement then associated with it that can convert to a UIElement, then convert it and parent it
6. If the data type of Content has a TypeConverter associated with it that can convert to a string then wrap associated with it that can convert to a string, then wrap it in a TextBlock and parent that
7. Finally, call ToString on the Content, then wrap it in a TextBlock and parent that.

ItemsPresenter for a list
• ContentPresenter is good for a single item, but not for a list of items
• ItemsPresenter is used to create multiple content for a list of items
• ItemsPresenter is the key of ItemsControl




• DispatcherObject
    The base class for any object that wishes to be accessed only on the thread that created it.
• DependencyObject
    The base class for any object that can support dependency properties.
• Visual
    The base class for all objects that have their own visual representation.
• UIElement
    The base class for all visual objects with support for routed events command binding layout keyboard mouse and focus events, command binding, layout,  keyboard, mouse and focus.
• FrameworkElement
    The base class that adds support for styles data binding The base class that adds support for styles, data binding, resources, and a few common mechanisms for Windows-based controls such as tooltips and context menus.
• Control
    The base class for familiar controls such as Button, ListBox, and StatusBar. Control adds many properties to its and StatusBar. Control adds many properties to its FrameworkElement base class, such as Foreground, Background, and FontSize. Controls also support templates that enable you to completely replace their visual tree
• Freezable
    The base class for objects that can be “frozen” into a read-only state for performance reasons.
• ContentElement
    A base class similar to UIElement, but for pieces of content that don’t have rendering behavior on their own.
• FrameworkContentElement
    The analog to FrameworkElement for content.


Dependency Property Dependency Property
• XAML, Style, Animation, DataBinding XAML, Style, Animation, DataBinding
• Value Change Notification
• Memory save (Button has 96(78) properties, Label has 89(71) properties)
• Property Value Inheritance
    – DP can support value inheritance by add FrameworkPropertyMetadataOptions.Inherits to DP.register
    – StatusBar and its children don’t support inherit
• Support for multiple providers


How to decide a DP s value
1 Local value 1. Local value
2. Style triggers
3. Template triggers
4. Style setters Sty e sette s
5. Theme style triggers
6. Theme style setters
7. Property value inheritance
8. Default value

Style Style
• ControlTemplate
    – No OnPaint/WM_Paint anymore
• Triggers
    – ControlTemplate p
    – Style
• Setters Setters
    – Setters are used to set a property value


why Dependency Property:memory save?

    依赖属性有一个默认值,这样这个依赖属性所属的控件的各个实例都共享这个依赖属性的值(条件是,不改变这个依赖属性

的默认值)。这样,众多的控件的属性在不改变默认值的情况实际在内存中只有一份,各个控件的依赖属性的值都指向内存中这个值。节

约了内存空间。但这个默认值一旦被用户修改,WPF会在内存中单独new出来一份。
原文地址:https://www.cnblogs.com/peach/p/1331014.html