Initializing controls within your WPF custom control template

Sometimes your custom control is a composite control where your template contains multiple WPF controls.
If you want to initialize those controls from code this cannot be done in the constructor of your custom control.
To do so, there is an override of the OnApplyTemplate method which is the best place to do Initialization.


   1: public override void OnApplyTemplate()
   2: {
   3:     Button myButton = this.Template.FindName("MyButton", this) as Button;
   4:     myButton.Click +=new RoutedEventHandler(myButton_Click);
   5:     myButton.Content = "Initialized";
   6: }
This entry was posted in WPF. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s