ScrollBar control styling tips

Ee341375.0b2c798e-86c6-4ea2-a8a6-5cf1f12be1e2(en-us,Expression.40).png

You can use the built-in ScrollBar control template to create a custom ScrollBar template. By default, the ScrollBar control template looks like the following:

Ee341375.3a7948bb-14c7-46cd-8098-11ee8579802d(en-us,Expression.40).png

Parts of the ScrollBar template

The ScrollBar template includes two sets of parts: one set for a vertical scroll bar, and one set for a horizontal scroll bar. The part names are prefixed by the word "vertical" or "horizontal," depending on the orientation of the scroll bar.

Tip

To view the parts of the template, open the Parts panel while modifying the template.

Ee341375.ea64f295-e640-448b-a3ef-c28ae681e977(en-us,Expression.40).png

Part name Object type

Ee341375.25182a96-9a69-478a-9cfe-5b360e6a9bea(en-us,Expression.40).png VerticalRoot

Ee341375.05fe4b1d-f584-4bd2-99b6-8bc2d735a89d(en-us,Expression.40).png HorizontalRoot

FrameworkElement

Ee341375.f0c1ff71-7814-42ba-806b-7ea92d616e69(en-us,Expression.40).png VerticalSmallDecrease

Ee341375.eb6fad93-f17e-4f62-a926-8c8651862891(en-us,Expression.40).png HorizontalSmallDecrease

RepeatButton

Ee341375.a5d608f2-bba2-48c5-8b15-2c115db86acc(en-us,Expression.40).png VerticalLargeDecrease

Ee341375.15de085f-48f5-41dd-a286-e3dcb4cfd18b(en-us,Expression.40).png HorizontalLargeDecrease

RepeatButton

Ee341375.1d2fe38a-3fd8-4951-b978-7e44b6f2401d(en-us,Expression.40).png VerticalThumb

Ee341375.1aa736bd-cd0d-4514-a5e4-b495d11d4870(en-us,Expression.40).png HorizontalThumb

Thumb

Ee341375.272b5149-46f2-4633-ae61-88dfb9c58b7e(en-us,Expression.40).png VerticalLargeIncrease

Ee341375.97fa60b9-0caf-4387-9225-b57510d32209(en-us,Expression.40).png HorizontalLargeIncrease

RepeatButton

Ee341375.b5acdd5b-dc01-4fec-a4b7-641b4cb36f50(en-us,Expression.40).png VerticalSmallIncrease

Ee341375.65bdc0a8-bb2a-4ad1-a136-70070743794a(en-us,Expression.40).png HorizontalSmallIncrease

RepeatButton

ScrollBar tips

Root part

This part is mandatory. The Root part contains all the elements that make up the ScrollBar in a particular orientation.

To make a Root part, create a grid named HorizontalRoot or VerticalRoot that contains five columns (for horizontal orientation) or rows (for vertical orientation) sized Auto, Auto, Auto, Star, Auto.

Ee341375.b6e16f2c-0ce1-4bc1-919f-e135448a6bd9(en-us,Expression.40).png

Thumb part

This part is mandatory. The thumb is the moveable element in the scrollbar.

To make the object or objects that represent the thumb into a Thumb control (HorizontalThumb or VerticalThumb), place the Thumb part in the middle column/row of the Root part, and set the Width and Height properties of the Thumb part so that the column (or row) will be resized to fit the thumb.

Do not set the Margin properties for the thumb. To get the same effect, set the margins of the root object of the Thumb template.

Track

The space in which the thumb moves is called the track. The track is not a part and is optional.

To include a track, place the object(s) representing the track into the Root part, either spanning all five columns (or rows) or spanning the middle three columns (or rows).

To make an object span multiple columns (or rows), select the object, and then set the ColumnSpan or RowSpan property in the Properties panel.

Moving the thumb along the track in large increments

If you want the user to be able to click the track on either side of the thumb to move the thumb in large increments, place a RepeatButton control in the two columns (or rows) on either side of the Thumb part and name them VerticalLargeDecrease and VerticalLargeIncrease (or HorizontalLargeDecrease and HorizontalLargeIncrease). So that the RepeatButtons function but are not seen, give them zero Opacity. Alternatively, apply a template that contains a single object with zero Opacity to the RepeatButton controls.

Moving the thumb along the track in small increments

If you want the user to be able to click a button at either end of the track to move the thumb in small increments, make the objects representing these buttons into RepeatButton controls and name them VerticalSmallDecrease and VerticalSmallIncrease (or HorizontalSmallDecrease and HorizontalSmallIncrease). Place them in the two end columns (or rows).

States of the ScrollBar control

You can also use states with the ScrollBar control template. By default, the ScrollBar control can be in one of the following three states in the CommonStates state group:

State name Description

Normal

The appearance of the ScrollBar control when there is no interaction with the control.

MouseOver

The appearance of the ScrollBar control when the user moves the pointer over it.

Disabled

The appearance of the ScrollBar control when the IsEnabled property is set to False.

Tip

To view the available states, open the States panel when modifying a ScrollBar template.

When you select a state, state recording is turned on, and any changes that you make will be recorded for that state. To turn off state recording, click the Record mode indicator Ee341375.0dcc6415-6d4e-4fcf-a9a3-eb4664cb6cbc(en-us,Expression.40).png, or select Base in the States panel.

For more information, see Defining different visual states for a control.

Template-binding

You can template-bind the Background, BorderBrush, Foreground, BorderThickness, or Padding properties. For more information, see Carry object properties through to the template.

To convert objects into a ScrollBar control

The following image is a design comprehensive (comp) of a scroll bar that includes MouseOver and Pressed states of the VerticalThumb, VerticalSmallDecrease, and VerticalSmallIncrease parts.

Normal

ScrollBar in Normal state

MouseOver

ScrollBar in MouseOver state

Pressed

ScrollBar in Pressed state

Disabled

ScrollBar in Disabled state

This example uses the XAML code in step 1 of the following procedure, which corresponds to the preceding graphics to create a custom scroll bar by using the ScrollBar control template.

Tip

Click Split View to see the changes in both Design view and Code view as you follow this procedure.

  1. Open a new Microsoft Silverlight project. In Code view, locate the following code, and then delete the closing slash mark (/).

    <Grid x:Name="LayoutRoot" Background="White"/>
    
  2. Copy and then paste the following code into your new project, after the code that you located in step 1.

    <Grid x:Name="verticalscrollbar" Width="17" Height="146" >
       <Rectangle x:Name="track" Fill="#FF3D3D3D" StrokeThickness="0" />
       <Path x:Name="smalldecrease" Fill="Gray" Stretch="Fill" Width="7" Height="7" 
          Data="M200,312 L204,312 L200,305 z" Margin="0,5,0,0" VerticalAlignment="Top"/>
       <Rectangle x:Name="thumb" Fill="Gray" RadiusX="3" RadiusY="3" Height="61" Width="7" 
          Margin="0,24,0,0" VerticalAlignment="Top"/>
       <Path x:Name="smallincrease" Fill="Gray" Stretch="Fill" Width="7" Height="7" 
          Data="M200,305 L200,312 L204,305 z" Margin="0,0,0,5" 
          VerticalAlignment="Bottom" />
    </Grid>
    
  3. Add a closing Grid tag (</Grid>) after the code that you just pasted.

  4. In the Objects and Timeline panel, right-click verticalscrollbar, and then click Make Into Control. In the Make Into Control dialog box, click ScrollBar, and then click OK.

    In Code view, note that the code that you pasted in the preceding step has been replaced with the code for a new ScrollBar control (locate the code beginning with <Grid x:Name="verticalscrollbar">). In addition, Microsoft Expression Blend turned verticalscrollbar into a template for a new ScrollBar style and applied that template to verticalscrollbar (locate <Style x:Key="ScrollBarStyle1" TargetType="ScrollBar">).

    Tip

    In the Objects and Timeline panel, note that UserControl has been replaced by Template.

  5. To make verticalscrollbar into the root of the template, in the Objects and Timeline panel, right-click verticalscrollbar, then click Make Into Part of ScrollBar, and then click VerticalRoot. Note that verticalscrollbar has been renamed VerticalRoot.

  6. To add rows to the Root part grid, in Design view, click the blue ruler to the left of the control to add new rows as illustrated in the following graphic:

    ScrollBar row dividers

    If you add the rows as shown, the parts will be in the correct rows. Track has a RowSpan of 5, smalldecrease is in Row 0, thumb is in Row 2, and smallincrease is in Row 4.

    Note

    Numbering begins at 0, so the 5 rows are numbered 0 through 4.

    You now have to define the elements of the artwork as parts of the template. As you define the elements, you can also assign values to specific properties.

  7. In the Objects and Timeline panel, right-click smalldecrease, click Make Into Part of ScrollBar, and then click VerticalSmallDecrease. In the Make Into Part dialog box, click OK.

    The smalldecrease element has just been made into a type of RepeatButton that is the VerticalSmallDecrease part of the ScrollBar control template.

  8. By default, Expression Blend creates a ContentPresenter for the part. In this example, you won't use the ContentPresenter, so you can delete it by right-clicking ContentPresenter in the Objects and Timeline panel, and then clicking Delete.

  9. To set the properties for the VerticalSmallDecrease part, in the Objects and Timeline panel, click smalldecrease, and then open the Properties panel. In the Layout category, set the Width and the Height to 7.

  10. To add states to the VerticalSmallDecrease part, in the States panel, click MouseOver. In the Properties panel, set the Fill to #FFCCCCCC. Return to the States panel, and then click Base to end state recording.

    Note

    You can also end state recording by clicking the Record mode indicator Ee341375.0dcc6415-6d4e-4fcf-a9a3-eb4664cb6cbc(en-us,Expression.40).png in the upper-left corner of the document window.

    In the States panel, click Pressed. In the Properties panel, set the Fill to #FFE5E5E5. Return to the States panel, and then click Base.

    In the States panel, click Normal, click Add Transition, and then click Normal to MouseOver. In the Transition Duration box, type .2, and then click Base.

  11. In the Objects and Timeline panel, click Grid. In the Brushes category in the Properties panel, click Solid Color Brush Ee341375.3a66ec96-47bb-47fc-8876-6b9456feec3a(en-us,Expression.40).png, and then set the Alpha property to 1.

  12. Go back to template-editing mode for the ScrollBar control [ScrollBarStyle1 (ScrollBar Template)] by clicking Return scope to Ee341375.55844eb3-ed98-4f20-aa66-a6f5b23eeb2b(en-us,Expression.40).png in the Objects and Timeline panel, or by clicking VerticalSmallDecrease in the breadcrumb bar at the top of the artboard.

    Repeat steps 7 through 12 for the smallincrease element.

  13. To define the thumb as part of the template, in the Objects and Timeline panel, right-click thumb, click Make Into Part of ScrollBar, and then click Vertical Thumb. In the Make Into Part dialog box, click OK.

  14. To set the properties for the VerticalThumb part, go back to template-editing mode for the ScrollBar control [ScrollBarStyle1 (ScrollBar Template)] by clicking Return scope to Ee341375.55844eb3-ed98-4f20-aa66-a6f5b23eeb2b(en-us,Expression.40).png, and then, in the Objects and Timeline panel, click VerticalThumb. In the Properties panel, in the Layout category, set the Width and the Height to 7. Set the Margin values as follows:

    • Left: 5

    • Right: 5

    • Top: 0

    • Bottom: 0

    Click Advanced Properties, and then set MinHeight to 7. This assigns a minimum height value to the thumb so that it is always visible.

  15. To add states to the thumb part, return to the Thumb template [ThumbStyle1(ThumbTemplate)] by clicking thumb in the breadcrumb bar at the top of the artboard. In the States panel, click MouseOver. In the Properties panel, set the Fill to #FFCCCCCC. Return to the States panel, and then click Base.

    In the States panel, click Pressed. In the Properties panel, set the Fill to #FFE5E5E5. Return to the States panel, and then click Base.

    In the States panel, click Normal, click Add Transition, and then click Normal to MouseOver. In the Transition Duration box, type .2, and then click Base.

  16. Go back to ScrollBarStyle1 (ScrollBar Template) by clicking Return scope to Ee341375.55844eb3-ed98-4f20-aa66-a6f5b23eeb2b(en-us,Expression.40).png in the Objects and Timeline panel. In the Objects and Timeline panel, click VerticalRoot. In the Parts panel, double-click VerticalLargeDecrease to create that part and to make it a child of VerticalRoot. In the Properties panel, set the Row to 1 and the Opacity to 0.

  17. In the Objects and Timeline panel, click VerticalRoot. In the Parts panel, double-click VerticalLargeIncrease to create that part and to make it a child of VerticalRoot. In the Properties panel, set the Row to 3 and the Opacity to 0.

  18. For rows 0, 1, 2, and 4, click the Star icon Ee341375.1b4edaf6-b6a8-4498-80dc-949375fa610d(en-us,Expression.40).png twice until the Auto icon Ee341375.aa9ec064-22f8-4b62-9eed-3f4772362d22(en-us,Expression.40).png appears. For row 3, make sure that the Star icon appears. This sets the heights of the rows as follows: Auto, Auto, Auto, Star, and Auto. Click each of the Auto-sized row dividers, and then, in the Properties panel, click Show advanced properties. To the right of the MinHeight value, click Advanced options, and then click Reset.

    ScrollBar rows resized

  19. In the Objects and Timeline panel, click VerticalRoot. In the States panel, click Disabled. In the Properties panel, set Opacity to 50.

  20. Build your project by clicking CTRL+SHIFT+B. When the build is complete, run your project by clicking F5, and then test your scroll bar.

References

You can find detailed information about the properties and events of the Silverlight ScrollBar control at the Silverlight Control Gallery Ee341375.xtlink_newWindow(en-us,Expression.40).png on MSDN.

See also

Tasks

Bind an object to user input or to other internal values

Concepts

Styling tips for common Silverlight controls
SimpleScrollBar
Styling a control that supports templates

Send feedback about this topic to Microsoft. © 2011 Microsoft Corporation. All rights reserved.