ViewportSize Property
Collapse the table of content
Expand the table of content

ScrollBar.ViewportSize Property

Updated: February 2009

Gets or sets the amount of the scrollable content that is currently visible. This is a dependency property.

Namespace:  System.Windows.Controls.Primitives
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

public double ViewportSize { get; set; }
<object ViewportSize="double" .../>

Property Value

Type: System.Double
The amount of the scrollable content that is currently visible. The default is 0.

Identifier field

ViewportSizeProperty

Metadata properties set to true

None

The value of the ViewportSize property is used to calculate the size of the Thumb control in the ScrollBar control. You typically set the ViewportSize property when the ScrollBar is part of a ControlTemplate by using template binding. For example, when the ScrollBar is in a ControlTemplate of a ScrollViewer control, you can template bind ViewportSize to ScrollViewer.ViewportHeight. The Thumb control's size represents the amount of a ScrollViewer control's content that is visible. If 25 percent of a ScrollViewer control's content is visible, the Thumb occupies 25 percent of the track in the ScrollBar.

You can decide which units ViewportSize uses. When you set this property, make sure that the ViewportSize, Minimum, Maximum, and Value properties use the same units.

The following illustration shows how the Thumb size reflects the amount of content that is visible.

The track length and thumbsize of a ScrollBar

The following example puts two ScrollBar controls in the ControlTemplate of a ScrollViewer. The ViewportSize property is template bound to the ScrollViewer.ViewportHeight property of the vertical ScrollBar. The ViewportSize property is template bound to the ScrollViewer.ViewportWidth property of the horizontal ScrollBar.

<Style x:Key="LeftScrollViewer" TargetType="{x:Type ScrollViewer}">
  <Setter Property="OverridesDefaultStyle" Value="True"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type ScrollViewer}">
        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition/>
          </Grid.ColumnDefinitions>
          <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
          </Grid.RowDefinitions>

          <ScrollContentPresenter Grid.Column="1"/>

          <ScrollBar Name="PART_VerticalScrollBar"
            Value="{TemplateBinding VerticalOffset}"
            Maximum="{TemplateBinding ScrollableHeight}"
            ViewportSize="{TemplateBinding ViewportHeight}"
            Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>
          <ScrollBar Name="PART_HorizontalScrollBar"
            Orientation="Horizontal"
            Grid.Row="1"
            Grid.Column="1"
            Value="{TemplateBinding HorizontalOffset}"
            Maximum="{TemplateBinding ScrollableWidth}"
            ViewportSize="{TemplateBinding ViewportWidth}"
            Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>

        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

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.

.NET Framework

Supported in: 3.5, 3.0

Date

History

Reason

February 2009

Clarified remarks about how to use ViewportSize. Also provided a new example.

Customer feedback.

Community Additions

ADD
Show:
© 2016 Microsoft