Create a new user control in your Silverlight 2 project
You can design your own customized controls that you can reuse multiple times in a Silverlight 2 project. These controls inherit from the UserControl class and therefore do not use templates. However, you have full control over the appearance of your user control by designing it from the ground up.
To create an empty user control
Open a Silverlight 2 project.
On the File menu, click New Item (CTRL+N).
In the New Item dialog box, under Installed templates, select UserControl.
Next to Name, enter a descriptive name for your user control.
If you want a code-behind file to be generated for this user control, select Include code file.
Tip
You will need a code-behind file if you want the user control to respond to user interaction, or if a developer will be creating custom behavior for the user control.
Click OK to close the dialog box and open the user control for editing.
You can now design the look of your user control.
To create a user control from existing objects
Select the existing objects that you want to make part of the new user control.
Tip
To select multiple objects, hold the CTRL key when you click each object.
On the Tools menu, click Make Control (F8).
In the Make Control dialog box, enter a descriptive name for your user control.
If you want to leave the selected objects where they are and created duplicates in the user control, select Leave original content as is and create duplicates as necessary.
Click OK to create the user control files and open the XAML file on the artboard for editing.
To draw a new instance of your user control in a document in your project
Save all your files (CTRL+SHIFT+S).
Build your project (CTRL+SHIFT+B) to make the user control appear in the Asset Library.
In the Toolbox, click Asset Library .
In the Asset Library, in the Custom Controls tab, select your user control.
Using your mouse, draw your user control on the artboard.
Tip
Use the Make Button command in Silverlight 2 projects to make a button from selected objects. You can select objects on the artboard, group them into a layout panel such as a Grid, and then click Make Button on the Tools menu. Make Button does not create a user control. Instead, it creates a button style that contains a template made up of the objects that you selected. To draw another button of the same kind, select your style in the Local Styles tab of the Asset Library and then draw on the artboard.
Troubleshooting
- When you use the Make Control command, if the objects that you selected include factored content such as event handlers or triggers, you may need to restore those event handler methods and triggers in the newly created user control.
Next steps
You can add more objects to your user control. For more information, see Draw a shape or path in your Silverlight 2 project, Draw a control in your Silverlight 2 project, or Add an image, audio, or video file to a project.
You can define custom states that your user control can be in, and change the appearance of your user control in each state. For more information, see Define different visual states and transition times for a user control.
You can make your user control respond to a mouse click by changing its appearance. For more information, see Change state in response to user interaction.
You can make your user control change its appearance in a more elaborate way when it is clicked. For more information, see Add animation that will play after a change in state.
You can make your user control appear at runtime in response to a button click or other user interaction. For more information, see Load a new page dynamically in your Silverlight 2 application.