Building a Simple Custom Control in Silverlight 2
With the recent announcement of the Silverlight Controls Builder Contest, I thought it may be useful to post code that creates a very simple custom control.
The example I’m posting isn’t even worth a screenshot as all the control does is render the text "Hello property_value_here". Something like:
The value of the sample is that it provides code and architecture to demonstrate the basics of creating a simple control. A few items to note:
- The control lives in a separate assembly as the application
- The generic.xaml file is used to define the default template of the control
- DefaultStyleKey must be set in order for your control to show up without a custom template defined
- A DependencyProperty is used to enable animation and binding
- You can change the value by entering text in the TextBlock and clicking the button.
For a deeper look at creating custom controls I would recommend Karen Corby’s 4 part Parts & States Model with VisualStateManager.