Freigeben über


Implicit and explicit tags in xaml

In WPF, some collection properties require you to specify an object element for the collection itself, e.g.:

    <Something>
      <Something.Children>
        <ChildrenCollection>
          …
        </ChildrenCollection>
      </Something.Children>
    </Something>

Other collection properties require that you not specify an object element, e.g.:

    <Something>
      <Something.Children>
          …
      </Something.Children>
    </Something>

Other properties, most notably .Resources, allow both syntaxes.

Why is that?  We've changed the rules of couple times over the years, but these days there's nothing magical about implicit and explicit collection tags, rather the behavior falls out of the xaml spec.  The first syntax happens when you have a collection property that can be set and defaults to null:

Private ChildrenCollection_children = null;
Public ChildrenCollection Children {
    Get {return _children;}
    Set {_children = value;}
}

The second syntax happens when the collection property provides a default collection object (the preferred way of doing things).  And the third syntax (either syntax allowed) happens when you have a settable collection property that has a non-null default value.

Comments

  • Anonymous
    September 07, 2006
    Thanks for explaining that Nick.  Where does the ContentProperty attribute fit into the picture?

    Thanks,
    Josh Smith
  • Anonymous
    September 07, 2006
    The content property attribute (or CPA) specifies which property object content should be put inside.  For instance, for listbox Items is the content property, so:
    <ListBox>
     ...
    </ListBox>

    And

    <ListBox>
     <ListBox.Items>
       ...
     </ListBox.Items>
    </ListBox>

    Are equivalent.