Files
Andy (Steve) De George be103da07e Replace various WPF include files with content (#1335)
* net-current-v30plus-md.md

* net-current-v40plus-md.md

* tla2sharptla-ui-md.md

* tla2sharptla-uiautomation-md.md

* tla2sharptla-winclient-md.md

* tla2sharptla-xaml-md.md

* tlasharptla-ui-md.md

* tlasharptla-uiautomation-md.md

* tlasharptla-winclient-md.md

* tlasharptla-xaml-md.md

* fix warnings
2022-03-16 12:14:11 -04:00

11 KiB

title, description, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title description ms.date dev_langs helpviewer_keywords ms.assetid
Controls Learn about Windows Presentation Foundation common UI components, referred to as controls, but which might not inherit from the Control class. 03/30/2017
csharp
vb
controls [WPF], about WPF controls
3f255a8a-35a8-4712-9065-472ff7d75599

Controls

WPF SDK continues to use the term "control" to loosely mean any class that represents a visible object in an application, it is important to note that a class does not need to inherit from the xref:System.Windows.Controls.Control class to have a visible presence. Classes that inherit from the xref:System.Windows.Controls.Control class contain a xref:System.Windows.Controls.ControlTemplate, which allows the consumer of a control to radically change the control's appearance without having to create a new subclass. This topic discusses how controls (both those that do inherit from the xref:System.Windows.Controls.Control class and those that do not) are commonly used in WPF.

Creating an Instance of a Control

You can add a control to an application by using either XAML. All controls can be created similarly.

[!code-xamlControlsOverview#1]

The following example creates the same application in code. For brevity, the creation of the xref:System.Windows.Controls.Grid, grid1, has been excluded from the sample. grid1 has the same column and row definitions as shown in the preceding XAML example.

[!code-csharpControlsOverview#2] [!code-vbControlsOverview#2]

Changing the Appearance of a Control

It is common to change the appearance of a control to fit the look and feel of your application. You can change the appearance of a control by doing one of the following, depending on what you want to accomplish:

Changing a Control's Property Value

Many controls have properties that allow you to change how the control appears, such as the xref:System.Windows.Controls.Control.Background%2A of a xref:System.Windows.Controls.Button. You can set the value properties in both XAML and code. The following example sets the xref:System.Windows.Controls.Control.Background%2A, xref:System.Windows.Controls.Control.FontSize%2A, and xref:System.Windows.Controls.Control.FontWeight%2A properties on a xref:System.Windows.Controls.Button in XAML.

[!code-xamlControlsOverview#3]

The following example sets the same properties in code.

[!code-csharpControlsOverview#4] [!code-vbControlsOverview#4]

Creating a Style for a Control

WPF gives you the ability to specify the appearance of controls wholesale, instead of setting properties on each instance in the application, by creating a xref:System.Windows.Style. The following example creates a xref:System.Windows.Style that is applied to each xref:System.Windows.Controls.Button in the application. xref:System.Windows.Style definitions are typically defined in XAML in a xref:System.Windows.ResourceDictionary, such as the xref:System.Windows.FrameworkElement.Resources%2A property of the xref:System.Windows.FrameworkElement.

[!code-xamlControlsOverview#5]

You can also apply a style to only certain controls of a specific type by assigning a key to the style and specifying that key in the Style property of your control. For more information about styles, see Styling and Templating.

Creating a ControlTemplate

A xref:System.Windows.Style allows you to set properties on multiple controls at a time, but sometimes you might want to customize the appearance of a xref:System.Windows.Controls.Control beyond what you can do by creating a xref:System.Windows.Style. Classes that inherit from the xref:System.Windows.Controls.Control class have a xref:System.Windows.Controls.ControlTemplate, which defines the structure and appearance of a xref:System.Windows.Controls.Control. The xref:System.Windows.Controls.Control.Template%2A property of a xref:System.Windows.Controls.Control is public, so you can give a xref:System.Windows.Controls.Control a xref:System.Windows.Controls.ControlTemplate that is different than its default. You can often specify a new xref:System.Windows.Controls.ControlTemplate for a xref:System.Windows.Controls.Control instead of inheriting from a control to customize the appearance of a xref:System.Windows.Controls.Control.

Consider the very common control, xref:System.Windows.Controls.Button. The primary behavior of a xref:System.Windows.Controls.Button is to enable an application to take some action when the user clicks it. By default, the xref:System.Windows.Controls.Button in WPF appears as a raised rectangle. While developing an application, you might want to take advantage of the behavior of a xref:System.Windows.Controls.Button--that is, by handling the button's click event--but you might change the button's appearance beyond what you can do by changing the button's properties. In this case, you can create a new xref:System.Windows.Controls.ControlTemplate.

The following example creates a xref:System.Windows.Controls.ControlTemplate for a xref:System.Windows.Controls.Button. The xref:System.Windows.Controls.ControlTemplate creates a xref:System.Windows.Controls.Button with rounded corners and a gradient background. The xref:System.Windows.Controls.ControlTemplate contains a xref:System.Windows.Controls.Border whose xref:System.Windows.Controls.Border.Background%2A is a xref:System.Windows.Media.LinearGradientBrush with two xref:System.Windows.Media.GradientStop objects. The first xref:System.Windows.Media.GradientStop uses data binding to bind the xref:System.Windows.Media.GradientStop.Color%2A property of the xref:System.Windows.Media.GradientStop to the color of the button's background. When you set the xref:System.Windows.Controls.Control.Background%2A property of the xref:System.Windows.Controls.Button, the color of that value will be used as the first xref:System.Windows.Media.GradientStop. For more information about data binding, see Data Binding Overview. The example also creates a xref:System.Windows.Trigger that changes the appearance of the xref:System.Windows.Controls.Button when xref:System.Windows.Controls.Primitives.ButtonBase.IsPressed%2A is true.

[!code-xamlControlsOverview#6]
[!code-xamlControlsOverview#7]

Note

The xref:System.Windows.Controls.Control.Background%2A property of the xref:System.Windows.Controls.Button must be set to a xref:System.Windows.Media.SolidColorBrush for the example to work properly.

Subscribing to Events

You can subscribe to a control's event by using either XAML or code, but you can only handle an event in code. The following example shows how to subscribe to the Click event of a xref:System.Windows.Controls.Button.

[!code-xamlControlsOverview#10]

[!code-csharpControlsOverview#8] [!code-vbControlsOverview#8]

The following example handles the Click event of a xref:System.Windows.Controls.Button.

[!code-csharpControlsOverview#9] [!code-vbControlsOverview#9]

Rich Content in Controls

Most classes that inherit from the xref:System.Windows.Controls.Control class have the capacity to contain rich content. For example, a xref:System.Windows.Controls.Label can contain any object, such as a string, an xref:System.Windows.Controls.Image, or a xref:System.Windows.Controls.Panel. The following classes provide support for rich content and act as base classes for most of the controls in WPF.

For more information about these base classes, see WPF Content Model.

See also