ScrollViewer control styling tips
The ScrollViewer control represents an object that displays a scrollable viewing area. Any objects can be placed in the viewing area. For example, inside the template of a ListBox, there is a ScrollViewer control that contains an ItemsPresenter that displays a list of items.
As with all controls, the ScrollViewer control can be modified to look very different from the default appearance. By default, the ScrollViewer control looks like the following:
ScrollViewer control properties of interest
To hide or show the ScrollBar objects in a ScrollViewer control, set the HorizontalScrollBarVisibility and VerticalScrollBarVisibility properties, which appear when you click Show advanced properties at the bottom of the Layout category of the Properties panel.
You can set these properties in the following ways:
Set the properties of the object After you draw a ScrollViewer object on the artboard, you can set the properties of the object directly. If you want several ScrollViewer objects to use the same values, set these properties in a style.
Set the properties in a style If you set these properties in the style of the ScrollViewer object, any ScrollViewer object that uses that style will use those values. You can overwrite the values for a specific object.
For more information, see Create a style.
To display an item in a ScrollViewer control, you can select it under Objects and Timeline to make it the active container, and then draw new objects or move existing objects into the ScrollViewer control.
If you want the ScrollViewer control to display items in a collection of data, consider using a ListBox control instead.
For more information, see Bind an object to data and Creating sample data.
Parts of the ScrollViewer template
The ScrollViewer control uses one template to define its appearance: the ScrollViewer Template. Each part of the template plays a role in the appearance and behavior of the ScrollViewer object to which the template is applied.
Other objects can exist in the template to dress up the appearance of the ScrollViewer control, but the parts listed in the following table are tied to its behavior in a contract.
To view the parts of the template, open the Parts panel while modifying the template.
Part name |
Object type |
Description |
---|---|---|
ScrollContentPresenter |
ScrollContentPresenter |
An object that displays the content of the ScrollViewer control. This part is mandatory. |
HorizontalScrollBar |
ScrollBar |
A ScrollBar object along the bottom of the ScrollViewer control. |
VerticalScrollBar |
ScrollBar |
A ScrollBar object along the right side of the ScrollViewer control. |
States of the ScrollViewer control
By default, the ScrollViewer control does not have any states defined. You can create your own states, or use the states of a control that includes a ScrollViewer control in their template, such as the ListBox control.
For more information, see Define different visual states and transition times for a user control and ListBox control styling tips.
To convert objects into a ScrollViewer control
To modify the template of a ScrollViewer control, do one of the following:
Draw a ScrollViewer control on the artboard, and then create a copy of the default template.
For more information, see Create or modify a template.
Design the look of your ScrollViewer control by drawing objects or importing art, and then use the Make Into Control command.
If you use the Make Into Control command, follow these steps to make sure that you create all the objects that the ScrollViewer template requires:
Group all the objects that you want to define the appearance of your ScrollViewer control into a Grid layout panel.
Select the new Grid object, and then, on the Tools menu, click Make Into Control.
In the dialog box that appears, select ScrollViewer, name your template, and select the location where the template will be stored.
For information about locations, see Create a resource.
After you click OK, Microsoft Expression Blend enters template-editing mode and displays the objects that make up your vertical ScrollViewer control. You can continue to modify your template in this mode. For example, add or modify objects, or select a state in the States panel to modify the appearance of your template in that state.
Delete the ContentPresenter object that was created in the template.
To add an object that will be assigned to the ScrollContentPresenter part of the template, double-click ScrollContentPresenter in the Parts panel. Move the new ScrollContentPresenter object to the position where you want it.
Tip
Alternatively, you could add a ScrollContentPresenter object and rename it to ScrollContentPresenter before converting your objects into a ScrollViewer control. When the Make Into Control command converts objects into a control, an object that has the same name and the same type as a template part is automatically converted into that part. If no object can be used, the part will appear in the Parts panel next to an icon that indicates that it is unused.
For more information, see Assign template parts to objects.
If you have art or other objects that you want to use for the HorizontalScrollBar and VerticalScrollBar parts of the template, do the following:
Group each set of objects into a layout panel.
Right-click one of the new layout panels, point to Make Into Part of ScrollViewer, and then click HorizontalScrollBar or VerticalScrollBar.
This will replace your objects with a ScrollBar object and use your objects to create the style and template that will be applied to the ScrollBar object.
In the Make into Part dialog box, type a name for the ScrollBar style that will be created, select a location where the style will be stored, and then click OK.
While in template-editing mode for the new ScrollBar object, you can continue to make modifications to your objects.
For more information, see ScrollBar control styling tips.
To go back to template-editing mode for the ScrollViewer, click Return scope to in the Objects and Timeline panel, or click HorizontalScrollBar or VerticalScrollBar in the breadcrumb bar at the top of the artboard.
Consider template-binding the following properties of objects in your template to properties of the ScrollViewer object that will eventually use your template:
Visibility Template-bind this property to the Visible property.
Maximum Template-bind this property to the Width property (for the HorizontalScrollBar object) or the Height property (for the VerticalScrollBar object).
For more information, see Carry object properties through to the template.
Consider binding some of the brush properties in your template:
Background
BorderBrush
BorderThickness
To exit template-editing mode, click [ScrollViewer] in the breadcrumb bar at the top of the artboard, or click Return scope to in the Objects and Timeline panel.
For information about applying your new ScrollViewer template to other ScrollViewer objects, see Apply or remove a resource.
References
You can find detailed information about the properties and events of the Microsoft Silverlight ScrollViewer control at the Silverlight Control Gallery on MSDN.
See also
Concepts
Styling tips for common Silverlight controls