Condividi tramite


Create a new user control in your project

You can design your own customized controls that you can reuse multiple times in a project. These controls inherit from the UserControl class and therefore do not use templates. However, you can control the appearance of your user control by designing it from scratch.

To create an empty user control

  1. Open a project.

  2. On the File menu, click New Item (Ctrl+N).

    JJ169827.b2bd3ff5-22c3-403e-af75-ed0d4d1b7aed(it-it,VS.110).png

  3. In the New Item dialog box, select UserControl.

  4. Next to Name, type a descriptive name for your user control.

  5. If you want a code-behind file to be generated for this user control, select Include code file.

    SuggerimentoSuggerimento

    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.

  6. 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

  1. Select the existing objects that you want to make part of the new user control.

    SuggerimentoSuggerimento

    To select multiple objects, hold down the Ctrl key when you click each object.

  2. On the Tools menu, click Make Into UserControl (F8).

    JJ169827.2a301fcf-3ece-4254-a3d1-654ad98cb9a7(it-it,VS.110).png

  3. In the Make Into UserControl dialog box, type a descriptive name for your user control.

  4. 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.

  5. 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

  1. Save all your files (Ctrl+Shift+S).

  2. Build your project (Ctrl+Shift+B) to make the user control appear in the Assets panel.

  3. In the Tools panel, click Assets JJ169827.0d8b8d29-1af9-418f-8741-be3097d76eab(it-it,VS.110).png.

  4. In the Assets panel, in the Project tab, select your user control.

    JJ169827.9cd1da53-f2d2-4f69-8b7d-3cce14b06763(it-it,VS.110).png

  5. Using your mouse, draw your user control on the artboard.

SuggerimentoSuggerimento

Use the Make Into Control command to make a control template from selected objects. You can select objects on the artboard, group them into a layout panel such as a Grid, and then click Make Into Control on the Tools menu. The Make Into Control command does not create a user control. Instead, it creates a control style that contains a template made up of the objects that you selected. To draw another control of the same kind, select your style in the Styles category of the Assets panel, and then draw on the artboard.

Troubleshooting

  • When you use the Make Into UserControl 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

Vedere anche

Concetti

Button control styling tips