Silverlight implements the control contract through a parts and states model. The parts and states model keeps a separation between logic and visuals, while still maintaining an explicit control contract between the designer and the developer. The main concepts in the parts and states model are parts, states, state groups and visual transitions.
You can find the primary named parts and states of a Silverlight control in the Syntax section of the control's class page. For example, you can find the named parts and states of a text box in the Syntax section of the TextBox topic. You can find a more complete list of the parts and states for most controls in the Control Styles and Templates topic. For example, the named parts and states for text box are listed in TextBox Styles and Templates.
Note: |
|---|
Using the parts and states model is the recommended way to create your control, but it is not required by the Silverlight runtime. However, if you want your control to open and render properly in Expression Blend, you must follow the supported parts and states model pattern. |
Parts
Parts are named elements inside the control template. The control logic expects these parts to be in the control template.
The following illustration shows the parts for the ComboBox control. The ComboBox control has five named parts: ContentPresenter, ContentPresenterBorder, DropDownToggle, ScrollViewer and Popup. Each is programmatically accessed by the control code. When the DropDownToggle is pressed, the Popup is opened and it displays the items in the ComboBox. When you click an item, it is displayed in the ContentPresenter.
.png)
States
Visual states are the way the control appears in a particular state. For example, the following illustration shows how a button has a different background color in the Normal, MouseOver and Pressed states.
.png)
State Groups
State groups are a set of mutually exclusive states. Different state groups are orthogonal. This means that a control can be in two different states at the same time only if the states belong to different state groups.
A CheckBox control has four state groups: CommonStates, CheckStates, FocusStates, and ValidationStates. Therefore, a CheckBox can be the in Normal and Checked state at the same time because these states belong to different state groups. But a CheckBox cannot be in Normal and Pressed states at the same time because both these states belong to the CommonStates state group. The following illustration shows CommonStates and CheckStates for CheckBox.
.png)
Visual Transitions
Visual transitions represent the visual appearance of a control as it goes from one state to another. In the following illustration, the background of the custom Button gradually changes from a lighter color to a darker color as it goes from the MouseOver to the Pressed state.
.png)