Reputation: 90465
In WPF should I include InitializeComponent()
in the user control constructor?
Upvotes: 2
Views: 1515
Reputation: 35584
InitializeComponent
binds the XAML-defined child controls (those with the attribute x:Name
) to the fields defined in the class. So, you should have InitializeComponent
at the class which uses XAML for definition. You cannot do this in the base class, because it doesn't know those fields. (Beside that InitializeComponent
parses the XAML and creates the visual elements, but this is not important for now).
This means, if your control is defined with XAML, you should have InitializeComponent. If you don't use XAML (but just overriding something in constructor or adding some more properties/features), you don't need InitializeComponent.
Upvotes: 3
Reputation: 70122
Yes, when this method is called, the XAML for your control is parsed and the various elements created.
Upvotes: 0