ScrollViewer control styling tips


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


Parts of the ScrollViewer template

The ScrollViewer template includes the following parts:

Part name Object type







tip noteTip

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

ScrollViewer tips

A ScrollViewer consists of scrollable content controlled by a horizontal ScrollBar and a vertical ScrollBar. A ScrollBar can be set to always be shown, to always be hidden, or to be shown only when needed.

ScrollViewerPresenter part

The ScrollContentPresenter part is mandatory. The ScrollContentPresenter displays the scrollable content

HorizontalScrollBar part

The HorizontalScrollBar part is optional. Template-bind this part's Visibility property to ComputedHorizontalScrollBarVisibility, its Maximum property to ScrollableWidth, its Value property to HorizontalOffset and its ViewportSize property to ViewportWidth.

VerticalScrollBar part

The VerticalScrollBar part is optional. Template-bind this part's Visibility property to ComputedVerticalScrollBarVisibility, its Maximum property to ScrollableHeight, its Value property to VerticalOffset and its ViewportSize property to ViewportHeight.


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

To convert objects into a ScrollViewer control

The following graphic is a design comprehensive (comp) of a scroll viewer created by a designer.


It is important to note that the preceding graphic is not yet a scroll viewer, but artwork that resembles a scroll viewer.

This example uses the XAML code in step 2 of the following 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="root" Height="146" Width="146">
       <Rectangle Fill="#FF333333" RadiusX="3" RadiusY="3" />
       <Rectangle x:Name="content" Stroke="Black" Width="200" Height="200">
             <GradientStop Color="White"/>
             <GradientStop Color="Black" Offset="1"/>
       <Grid x:Name="verticalscrollbar" Width="17" Height="146" HorizontalAlignment="Right">
          <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" 
          <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" />
  3. Add a closing Grid tag (</Grid>) after the code that you just pasted.

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

    In this step, Expression Blend removes root and everything inside it, and puts a new ScrollViewer in its place. Next, Expression Blend turns root into the template of a new ScrollViewer style and applies that new style to the new ScrollViewer.

  5. In the Objects and Timeline panel, right-click ContentPresenter, and then click Delete.

  6. In the Objects and Timeline panel, click root. In the Parts panel, double-click ScrollContentPresenter.

  7. Right-click verticalscrollbar, point to Make Into Part of ScrollViewer, and then click VerticalScrollBar. Click OK.

    To style the VerticalScrollBar, see ScrollBar control styling tips.

  8. Click Return scope to Ee341456.55844eb3-ed98-4f20-aa66-a6f5b23eeb2b(en-us,Expression.40).png to return to ScrollViewerStyle1 (ScrollViewer template).

  9. VerticalScrollBar must be able to scroll the viewport of the scrollable area. To make this possible, you can use the Custom Expression option on the Advanced options menu in the Properties panel. Set Visibility to {TemplateBinding ComputedVerticalScrollBarVisibility}, Maximum to {TemplateBinding ScrollableHeight}, Value to {TemplateBinding VerticalOffset} and ViewportSize to {TemplateBinding ViewportHeight}.

    Alternatively, in Split view, with VerticalScrollBar selected in the Objects and Timeline panel, locate the line of code that begins <ScrollBar x:Name="VerticalScrollBar, and then paste the following code at the end of the line that you have just located.

    Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" 
    Maximum="{TemplateBinding ScrollableHeight}" 
    Value="{TemplateBinding VerticalOffset}" 
    ViewportSize="{TemplateBinding ViewportHeight}"
  10. ScrollContentPresenter and VerticalScrollBar have to be side by side, with each in its own column. In the Objects and Timeline panel, click root. On the artboard, click the blue border above the control, and then drag the orange arrow until it is right next to the scroll bar, as illustrated in the following image:

    ScrollViewer with columns
  11. For the right column, click Star Ee341456.1b4edaf6-b6a8-4498-80dc-949375fa610d(en-us,Expression.40).png two times. The Auto icon Ee341456.aa9ec064-22f8-4b62-9eed-3f4772362d22(en-us,Expression.40).png appears.

  12. In the Objects and Timeline panel, click ScrollContentPresenter. In the Layout category of the Properties panel, click Advanced options to the right of ColumnSpan, and then click Reset.

  13. In the Objects and Timeline panel, click VerticalScrollBar. In the Layout category of the Properties panel, click Advanced options to the right of Margin, and then click Reset.

  14. The object named content is still in the template. This object is the 200-pixel square Rectangle over which the 146-pixel-square ScrollViewer is meant to scroll. The next step is to cut it out of the template and paste it into the Content property of the ScrollViewer. ScrollContentPresenter will then display it. In the Objects and Timeline panel, right-click content, and then click Cut.

  15. Click Return scope to Ee341456.55844eb3-ed98-4f20-aa66-a6f5b23eeb2b(en-us,Expression.40).png, and then press CTRL+V to paste content into the Content property of the ScrollViewer.

  16. Build your project (CTRL+SHIFT+B), and then test your project by pressing F5.

    important noteImportant

    The ListBox will not work as expected unless you have styled the ScrollBar by using the ScrollBar control styling tips as described in step 7.

For information about applying your new ComboBox template to other ComboBox objects, see Apply or remove a resource.


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

See also

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