Jader Dias
Jader Dias

Reputation: 90465

Should UserControls have InitializeComponent in its constructor?

In WPF should I include InitializeComponent() in the user control constructor?

Upvotes: 2

Views: 1515

Answers (2)

Vlad
Vlad

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

ColinE
ColinE

Reputation: 70122

Yes, when this method is called, the XAML for your control is parsed and the various elements created.

Upvotes: 0

Related Questions