ScrollViewer Class
Updated: February 2009
Represents a scrollable area that can contain other visible elements.
Assembly: PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
'Declaration <LocalizabilityAttribute(LocalizationCategory.Ignore)> _ <TemplatePartAttribute(Name := "PART_HorizontalScrollBar", Type := GetType(ScrollBar))> _ <TemplatePartAttribute(Name := "PART_VerticalScrollBar", Type := GetType(ScrollBar))> _ <TemplatePartAttribute(Name := "PART_ScrollContentPresenter", Type := GetType(ScrollContentPresenter))> _ Public Class ScrollViewer _ Inherits ContentControl 'Usage Dim instance As ScrollViewer
<ScrollViewer> Content </ScrollViewer>
A ScrollViewer enables content to be displayed in a smaller area than its actual size. When the content of the ScrollViewer is not entirely visible, the ScrollViewer displays scrollbars that the user can use to move the content areas that are visible. The area that includes all of the content of the ScrollViewer is the extent. The visible area of the content is the viewport.
Physical scrolling is used to scroll content by a predetermined physical increment, typically by a value that is declared in pixels. Logical scrolling is used to scroll to the next item in the logical tree. If you require physical scrolling instead of logical scrolling, wrap the host Panel element in a ScrollViewer and set its CanContentScroll property to false. Physical scrolling is the default scroll behavior for most Panel elements.
If your ScrollViewer contains a large number of items, the scrolling performance may be affected. In this case, set IsDeferredScrollingEnabled to true. This causes the content view to remain static while dragging the Thumb and to update only when the Thumb is released.
Because the scroll bars for a ScrollViewer element are defined in the default style of the element, scroll bars will no longer appear if you apply a custom style to a ScrollViewer. Scroll bars must be defined in the custom style for them to appear. For an example of how to implement scroll bars in a custom style, see ScrollViewer Style Sample.
Dependency properties for this control might be set by the control’s default style. If a property is set by a default style, the property might change from its default value when the control appears in the application. The default style is determined by which desktop theme is used when the application is running. For more information, see Themes.
This example shows how to create and use a ScrollViewer element in Extensible Application Markup Language (XAML).
The following example creates a window that contains some text and a rectangle. The scroll bars appear only when they are needed. When you resize the window, the scroll bars appear and disappear.
For the complete sample, see ScrollViewer Sample.
'Define a ScrollViewer. Dim myScrollViewer As New ScrollViewer myScrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto 'Add Layout control. Dim myStackPanel As New StackPanel myStackPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Left myStackPanel.VerticalAlignment = System.Windows.VerticalAlignment.Top Dim myTextBlock As New TextBlock myTextBlock.TextWrapping = TextWrapping.Wrap myTextBlock.Margin = New Thickness(0, 0, 0, 20) myTextBlock.Text = "Scrolling is enabled when it is necessary. Resize the Window, making it larger and smaller." Dim myRectangle As New Rectangle myRectangle.Fill = Brushes.Red myRectangle.Width = 500 myRectangle.Height = 500 'Add child elements to the parent StackPanel. myStackPanel.Children.Add(myTextBlock) myStackPanel.Children.Add(myRectangle) 'Add the StackPanel as the lone Child of the Border myScrollViewer.Content = myStackPanel Me.Content = myScrollViewer
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowTitle="ScrollViewer Sample"> <ScrollViewer HorizontalScrollBarVisibility="Auto"> <StackPanel VerticalAlignment="Top" HorizontalAlignment="Left"> <TextBlock TextWrapping="Wrap" Margin="0,0,0,20">Scrolling is enabled when it is necessary. Resize the window, making it larger and smaller.</TextBlock> <Rectangle Fill="Red" Width="500" Height="500"></Rectangle> </StackPanel> </ScrollViewer> </Page>
More Code
| How to: Use the Content-Scrolling Methods of ScrollViewer | This example shows how to use the scrolling methods of the ScrollViewer element. These methods provide incremental scrolling of content, either by line or by page, in a ScrollViewer. |
| How to: Handle the ScrollChanged Event | This example shows how to raise the ScrollChanged event of a ScrollViewer. |
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Date | History | Reason |
|---|---|---|
February 2009 | Described how default styles change dependency properties. | Customer feedback. |
July 2008 | Added new members: IsDeferredScrollingEnabled property, GetIsDeferredScrollingEnabled method, SetIsDeferredScrollingEnabled method, ContentVerticalOffset property, ContentHorizontalOffset property, ContentVerticalOffsetProperty field, ContentHorizontalOffsetProperty field, IsDeferredScrollingEnabledProperty field. | SP1 feature change. |