Analysis of each template in WPF (GO)

Source: Internet
Author: User

As a novice, still did not read this article, save later to learn in the use of TabControl, ListView, Menu, the TreeView when the various template to get dizzy, determined to make this problem clear, when should I use what template? This is a troublesome problem, and the way to do it is "according to my experience of writing programs for so many years, this is a problem that requires experience to solve". First look at the hierarchy of several related classes:
Control
|
+----ContentControl
| |
| +----ListBoxItem
| |
| +----HeaderedContentControl
| |
| +----TabItem
|
+----ItemsControl
|
+----TreeView
|
+----Menubase
|
+----HeaderedItemsControl
| |
| +----MenuItem
| |
| +----TreeViewItem
|
+----Selector
|
+----TabControl
|
+----ListBox There are four categories of interest, and for the sake of simplicity, give them a re-name:
    1. ContentControl: Single-element container without headers
    2. HeaderedContentControl: Single-element container with headers
    3. ItemsControl: Untitled Multi-Element container
    4. HeaderedItemsControl: Multi-element container with title
Each class has a different template, causing my headache symptoms of a total of 3, also give them a re-name:
    1. ContentTemplate: A single-element container draws a brush for a list element
    2. ItemTemplate: A multi-element container draws a brush for each child element
    3. HeaderTemplate: A brush that has the title of the container drawing title
Well, everything is clear so far. Let's look at the brushes for each class:
    1. ContentControl: A single-element container with no caption, only a brush that draws a list element
    2. HeaderedContentControl: A single-element container with a caption, obviously, a brush that has a more drawn caption than the one above
    3. ItemsControl: Untitled multi-element container with only one paint brush for each child element ItemTemplate
    4. HeaderedItemsControl: A multi-element container with a title, it is also obvious that a brush with a painting title
Likewise, everything is taken for granted and is obvious. At this point, however, confusion arises: when the "container" and "element" are paired together, the various brushes begin to become complex. Now to analyze several typical container and element combinations:

1, TabControl and TabItem

TabControl itself is an untitled multi-element container, as described above, there is no title brush, only one painting each child element of the ItemTemplate brush.
The element in his belly is tabitem, a single-element container with a title, with two brushes, contenttemplate and HeaderTemplate. So what is the relationship between the TabItem brush and the TabControl brush?
In fact I lied: TabControl has two brushes, contenttemplate and ItemTemplate, not one, where ItemTemplate inherits from the parent class, and ContentTemplate is a property that he redefined himself-- Okay, we finally caught this fake ID card. The guy who disturbed the social order and destroyed the harmonious society. This practice is confusing, but it brings convenience, and the correspondence between the container and the element is
    • TabItem's HeaderTemplate is TabControl's ItemTemplate.
    • TabItem's ContentTemplate is TabControl's contenttemplate.

2. Menu and MenuItem

Menu only one Itemtemplate,menuitem has HeaderTemplate and ItemTemplate. There are actually two matches, one is the menu and MenuItem, the other is MenuItem and MenuItem collocation. In these two combinations, there are the following things in common:
    • The ItemTemplate of the container becomes the headertemplate of the element
    • Each MenuItem uses its own corresponding headertemplate to show himself

This is a hierarchical structure, WPF provides hierarchicaldatatemplate, very convenient, but for the time being not to say this, lest the problem more complicated.

3. TreeView and TreeViewItem

As can be seen from the above class hierarchy, this collocation and menu/menuitem situation should be the same, in fact, in XAML is easy to switch between the TreeView and the menu, it is easy to change only a few tags can be seen, the two are "isomorphic".

4. ListBox and ListBoxItem

The listbox itself is an untitled multi-element container with only one ItemTemplate. The listbox is a good comrade to obey the law, unlike TabControl forged a contenttemplate. His ItemTemplate is ListBoxItem ContentTemplate, and ListBoxItem has no other brushes, and this pair is the simplest.

Analysis of each template in WPF (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.