Reputation: 13
I am reading an MSDN article for learning XAML: https://learn.microsoft.com/en-us/dotnet/desktop/wpf/fundamentals/xaml?view=netdesktop-5.0#content-properties-and-collection-syntax-combined
It says the collection's element tag can be omitted, based on the XAML rules for processing collections such as IList.
I can't find the XAML rules for processing collection that it says. I have searched for an hour to find out this rule. I can only find out information
"The Children collection of a Panel element can only consist of UIElement objects. Adding a UIElement child to a Panel implicitly adds it to the UIElementCollection for the Panel element."
in this article: https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.panel.children?view=net-5.0
Please let me know what is XAML rules for processing collection.
Upvotes: 0
Views: 642
Reputation: 70671
There are three things going on here, all described by the documentation page you referenced, but in some cases elaborated elsewhere:
StackPanel.Children
element may be omitted. This is because the Panel
class, which StackPanel
inherits, uses the [ContentProperty]
attribute to indicate that the Children
property is to be used when content is specified without an element tag.Panel.Children
property has the type UIElementCollection
, which implements IList
, and the XAML syntax rules specify that the XAML compiler will implicitly add items declared as content elements to the existing collection for the current element. This is documented by the Collections and collection types for XAML page. Note that in this case, the collection itself is implicitly specified, per the previous point.UIElementCollection
element tag, because this would indicate the instantiation of a whole new collection object to be assigned to the Children
property, but the property itself is read-only, so this is prohibited. I find the documentation's explanation (which it provides parenthetically in the passage you show in your post) misleading; it is correct to note that the class has no parameterless constructor, but there are ways in XAML around that limitation. The real problem is that even if one could construct a new instance of the collection, it can't be assigned to the Children
property.Upvotes: 1