ScrollViewer 类
TOC
折叠目录
展开目录
本文档已存档,并且将不进行维护。

ScrollViewer 类

Represents a scrollable area that can contain other visible elements.

命名空间: System.Windows.Controls
程序集: PresentationFramework(在 presentationframework.dll 中)
XML 命名空间:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

[LocalizabilityAttribute(LocalizationCategory.Ignore)] 
[TemplatePartAttribute(Name="PART_HorizontalScrollBar", Type=typeof(ScrollBar))] 
[TemplatePartAttribute(Name="PART_VerticalScrollBar", Type=typeof(ScrollBar))] 
[TemplatePartAttribute(Name="PART_ScrollContentPresenter", Type=typeof(ScrollContentPresenter))] 
public class ScrollViewer : ContentControl
/** @attribute LocalizabilityAttribute(LocalizationCategory.Ignore) */ 
/** @attribute TemplatePartAttribute(Name="PART_HorizontalScrollBar", Type=System.Windows.Controls.Primitives.ScrollBar) */ 
/** @attribute TemplatePartAttribute(Name="PART_VerticalScrollBar", Type=System.Windows.Controls.Primitives.ScrollBar) */ 
/** @attribute TemplatePartAttribute(Name="PART_ScrollContentPresenter", Type=System.Windows.Controls.ScrollContentPresenter) */ 
public class ScrollViewer extends ContentControl
LocalizabilityAttribute(LocalizationCategory.Ignore) 
TemplatePartAttribute(Name="PART_HorizontalScrollBar", Type=System.Windows.Controls.Primitives.ScrollBar) 
TemplatePartAttribute(Name="PART_VerticalScrollBar", Type=System.Windows.Controls.Primitives.ScrollBar) 
TemplatePartAttribute(Name="PART_ScrollContentPresenter", Type=System.Windows.Controls.ScrollContentPresenter) 
public class ScrollViewer extends ContentControl
<ScrollViewer>
  Content
</ScrollViewer>

The ScrollViewer element encapsulates a content element and up to two ScrollBar controls.

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.

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 Style the ScrollViewer Element.

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 Scroll Viewer Sample.


// Create the application's main window
mainWindow = new Window ();
mainWindow.Title = "ScrollViewer Sample";

// Define a ScrollViewer
myScrollViewer = new ScrollViewer();
myScrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;

// Add Layout control
myStackPanel = new StackPanel();
myStackPanel.HorizontalAlignment = HorizontalAlignment.Left;
myStackPanel.VerticalAlignment = VerticalAlignment.Top;

TextBlock myTextBlock = 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.";

Rectangle myRectangle = 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;

// Add the Border as the Content of the Parent Window Object
mainWindow.Content = myScrollViewer;
mainWindow.Show ();


<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>

更多代码

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: Raise the ScrollChanged Event

This example shows how to raise the ScrollChanged event of a ScrollViewer.

System.Object
   System.Windows.Threading.DispatcherObject
     System.Windows.DependencyObject
       System.Windows.Media.Visual
         System.Windows.UIElement
           System.Windows.FrameworkElement
             System.Windows.Controls.Control
               System.Windows.Controls.ContentControl
                System.Windows.Controls.ScrollViewer

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

Windows Vista、Microsoft Windows XP SP2 和 Windows Server 2003 SP1 支持 Microsoft .NET Framework 3.0。

.NET Framework

受以下版本支持:3.0
显示:
© 2016 Microsoft