14

According to MSDN - Panel.InternalChildren Property:

Classes that are derived from Panel should use this property, instead of the Children property, for internal overrides such as MeasureCore and ArrangeCore.


So, this is really a 2 part question:

  1. If I create a Panel of my own, FooPanel, which derives from Panel, I can't seem to override MeasureCore or ArrangeCore. I'm not sure why that statement is even there. I can, however, override MeasureOverride and ArrangeOverride. So, I wonder if I still need to use the InternalChildren property for these 2 methods.

  2. What is the real difference between the Children property and the InternalChildren property?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
myermian
  • 31,823
  • 24
  • 123
  • 215

2 Answers2

14
  1. You would override MeasureOverride and ArrangeOverride, that must be a mistake in the documentation, or intended for internal Microsoft employees. The MeasureCore and ArrangeCore are sealed by FrameworkElement, so you can't override them.

  2. The Children property is public and simply calls InternalChildren, which is protected. So either is probably safe, since Children would get inlined.

MSDN says otherwise ( http://msdn.microsoft.com/en-us/library/ms754152.aspx) but the documentation is wrong. (use reflector to see that the implementation of Children simply calls InternalChildren)

Elad Katz
  • 7,483
  • 5
  • 35
  • 66
CodeNaked
  • 40,753
  • 6
  • 122
  • 148
  • +1, I just looked at the code with dotPeek, after reading about Children vs InternalChildren in Petzold's book, where incidentally he uses MeasureOverride and ArrangeOverride but with InternalChildren. – RichardOD Dec 03 '11 at 15:00
  • children and internalchildren are not equivalent – Elad Katz Feb 09 '12 at 15:44
  • Or we look at the source: http://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Controls/Panel.cs#171 – Glenn Maynard May 24 '14 at 18:30
  • This is only true for the default implementations, which as the code proves, simply returns InternalChildren. However, if you are subclassing a panel, your panel may add extra items above and beyond those that are part of the Children collection (say a header or supporting views, etc.) In that case, they should be added to the InternalChildren collection, but you should also maintain your own Children collection which represents only the bound children. By default, the panels provided by the framework don’t add anything besides Children, hence it just returns InternalChildren directly. – Mark A. Donohoe Feb 07 '20 at 20:24
  • @MarkA.Donohoe - I don't believe you can override `Children` so it would always be the same as `InternalChildren`. Or are you referring to deriving at a higher level? – CodeNaked Feb 10 '20 at 19:41
  • 1
    Hmmm... good point! My understanding was if you, say, added a child yourself into the ‘Children’ collection, you could internally, say, add another child to ‘label’ the first one, etc. In that case Children would contain just the one you added but InternalChildren would contain both. However, as you said, you can’t override Children, so now I’m just as confused as the OP! – Mark A. Donohoe Feb 10 '20 at 19:49
3

EDIT: As CodeNaked corrected - MSDN docs are in fact incorrect. InternalChildren and Children are the same.

Using reflector, you can see the implementation of Children which is public UIElementCollection Children { get { return this.InternalChildren; } }. So unless there is some voodoo going on, they are the same.


Children are just children that were added regularly, whereas InternalChildren includes children that were added through data binding (when the panel is the ItemsPanelTemplate)

"Children represents the child collection of elements that the Panel is comprised of. InternalChildren represents the content of the Children collection plus those members generated by data binding. Both consist of a UIElementCollection of child elements hosted within the parent Panel."

see http://msdn.microsoft.com/en-us/library/ms754152.aspx

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Elad Katz
  • 7,483
  • 5
  • 35
  • 66