ScrollItemPattern.ScrollIntoView 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
捲動容器物件的內容區域,以顯示此容器之可見區域 (檢視區) 內的 AutomationElement。
public:
void ScrollIntoView();
public void ScrollIntoView ();
member this.ScrollIntoView : unit -> unit
Public Sub ScrollIntoView ()
例外狀況
無法將此項目捲動到檢視。
範例
在下列範例中, Select 已宣告 、 AddToSelection 或 RemoveFromSelection 事件處理常式,嘗試將物件卷 SelectionItemPattern 動至其容器控制項的可檢視區域。
///--------------------------------------------------------------------
/// <summary>
/// Obtains a ScrollItemPattern control pattern from an
/// automation element.
/// </summary>
/// <param name="targetControl">
/// The automation element of interest.
/// </param>
/// <returns>
/// A ScrollItemPattern object.
/// </returns>
///--------------------------------------------------------------------
private ScrollItemPattern GetScrollItemPattern(
AutomationElement targetControl)
{
ScrollItemPattern scrollItemPattern = null;
try
{
scrollItemPattern =
targetControl.GetCurrentPattern(
ScrollItemPattern.Pattern)
as ScrollItemPattern;
}
// Object doesn't support the ScrollItemPattern control pattern
catch (InvalidOperationException)
{
return null;
}
return scrollItemPattern;
}
'''--------------------------------------------------------------------
''' <summary>
''' Obtains a ScrollItemPattern control pattern from an
''' automation element.
''' </summary>
''' <param name="targetControl">
''' The automation element of interest.
''' </param>
''' <returns>
''' A ScrollItemPattern object.
''' </returns>
'''--------------------------------------------------------------------
Private Function GetScrollItemPattern( _
ByVal targetControl As AutomationElement) As ScrollItemPattern
Dim scrollItemPattern As ScrollItemPattern = Nothing
Try
scrollItemPattern = DirectCast( _
targetControl.GetCurrentPattern( _
scrollItemPattern.Pattern), ScrollItemPattern)
Catch
' Object doesn't support the ScrollItemPattern control pattern
Return Nothing
End Try
Return scrollItemPattern
End Function 'GetScrollItemPattern
///--------------------------------------------------------------------
/// <summary>
/// A Select, AddToSelection or RemoveFromSelection event handler that
/// scrolls a SelectionItem object into the viewable region of its
/// container control.
/// </summary>
///--------------------------------------------------------------------
private void OnSelectionItemSelect(
object src, SelectionChangedEventArgs e)
{
AutomationElement selectionItem = src as AutomationElement;
ScrollItemPattern scrollItemPattern = GetScrollItemPattern(selectionItem);
if (scrollItemPattern == null)
{
return;
}
try
{
scrollItemPattern.ScrollIntoView();
}
catch (InvalidOperationException)
{
// The item cannot be scrolled into view.
// TO DO: error handling.
}
}
'''--------------------------------------------------------------------
''' <summary>
''' A Select, AddToSelection or RemoveFromSelection event handler that
''' scrolls a SelectionItem object into the viewable region of its
''' container control.
''' </summary>
'''--------------------------------------------------------------------
Private Sub OnSelectionItemSelect( _
ByVal src As Object, ByVal e As SelectionChangedEventArgs)
Dim selectionItem As AutomationElement = _
DirectCast(src, AutomationElement)
Dim scrollItemPattern As ScrollItemPattern = _
GetScrollItemPattern(selectionItem)
If scrollItemPattern Is Nothing Then
Return
End If
Try
scrollItemPattern.ScrollIntoView()
Catch exc As InvalidOperationException
' The item cannot be scrolled into view.
' TO DO: error handling.
End Try
End Sub
備註
這個方法無法指定容器的可見區域 (檢視區) 的位置 AutomationElement 。