-api-id | -api-type |
---|---|
T:Windows.UI.Xaml.Controls.UserControl |
winrt class |
Provides the base class for defining a new control that encapsulates related existing controls and provides its own logic.
<UserControl ...>
singleContentElement
</UserControl>
-or-
<UserControl .../>
Note that UserControl does not manifest the value of its property in its control template. Instead, set the Background of the root element inside the UserControl. For more info, see Remarks in Control.Background.
The following examples demonstrate creating a UserControl and using it multiple times in an app. This first example creates a UserControl called NameReporter
that asks for a name of a person, and reports it back to the user. NameReporter
has several TextBlock controls, two TextBox controls, and a Button. The user enters a first and last name into the appropriate TextBox, and then clicks the button. The control then displays a message box with the name that the user entered.
[!code-xaml1]
[!code-csharp2]
[!code-vb2]
Note
The src
prefix in this XAML references the app itself and the UserControlExample
code namespace within it. That is where the NameReporter
control is defined. For more info on XAML prefixes, see XAML namespaces and namespace mapping.
[!code-xaml3]