GridItemPattern.GridItemPatternInformation.ContainingGrid 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得支援 和 表示儲存格或專案容器的 UI Automation 專案 GridPattern 。
public:
property System::Windows::Automation::AutomationElement ^ ContainingGrid { System::Windows::Automation::AutomationElement ^ get(); };
public System.Windows.Automation.AutomationElement ContainingGrid { get; }
member this.ContainingGrid : System.Windows.Automation.AutomationElement
Public ReadOnly Property ContainingGrid As AutomationElement
屬性值
支援 GridPattern 並代表資料表單元格或專案容器的 UI Automation 元素。 預設值為 Microsoft Visual Basic .NET) 中的 null 參考 Nothing
(。
範例
在下列範例中, AutomationFocusChangedEvent 會宣告接聽程式來追蹤格線容器內的格線專案周遊。 每個焦點變更事件時,專案屬性都會回應主控台。
///--------------------------------------------------------------------
/// <summary>
/// Obtains a GridItemPattern control pattern from an
/// automation element.
/// </summary>
/// <param name="targetControl">
/// The automation element of interest.
/// </param>
/// <returns>
/// A GridItemPattern object.
/// </returns>
///--------------------------------------------------------------------
private GridItemPattern GetGridItemPattern(
AutomationElement targetControl)
{
GridItemPattern gridItemPattern = null;
try
{
gridItemPattern =
targetControl.GetCurrentPattern(
GridItemPattern.Pattern)
as GridItemPattern;
}
// Object doesn't support the
// GridPattern control pattern
catch (InvalidOperationException)
{
return null;
}
return gridItemPattern;
}
'''--------------------------------------------------------------------
''' <summary>
''' Obtains a GridItemPattern control pattern from an
''' automation element.
''' </summary>
''' <param name="targetControl">
''' The automation element of interest.
''' </param>
''' <returns>
''' A GridItemPattern object.
''' </returns>
'''--------------------------------------------------------------------
Private Function GetGridItemPattern( _
ByVal targetControl As AutomationElement) As GridItemPattern
Dim gridItemPattern As GridItemPattern = Nothing
Try
gridItemPattern = DirectCast( _
targetControl.GetCurrentPattern(gridItemPattern.Pattern), _
GridItemPattern)
Catch exc As InvalidOperationException
' Object doesn't support the
' GridPattern control pattern
Return Nothing
End Try
Return gridItemPattern
End Function 'GetGridItemPattern
///--------------------------------------------------------------------
/// <summary>
/// Obtains a GridPattern control pattern from an
/// automation element.
/// </summary>
/// <param name="targetControl">
/// The automation element of interest.
/// </param>
/// <returns>
/// A GridPattern object.
/// </returns>
///--------------------------------------------------------------------
private GridPattern GetGridPattern(
AutomationElement targetControl)
{
GridPattern gridPattern = null;
try
{
gridPattern =
targetControl.GetCurrentPattern(
GridPattern.Pattern)
as GridPattern;
}
// Object doesn't support the
// GridPattern control pattern
catch (InvalidOperationException)
{
return null;
}
return gridPattern;
}
'''--------------------------------------------------------------------
''' <summary>
''' Obtains a GridPattern control pattern from an
''' automation element.
''' </summary>
''' <param name="targetControl">
''' The automation element of interest.
''' </param>
''' <returns>
''' A GridPattern object.
''' </returns>
'''--------------------------------------------------------------------
Private Function GetGridPattern( _
ByVal targetControl As AutomationElement) As GridPattern
Dim gridPattern As GridPattern = Nothing
Try
gridPattern = DirectCast( _
targetControl.GetCurrentPattern(gridPattern.Pattern), GridPattern)
Catch exc As InvalidOperationException
' Object doesn't support the
' GridPattern control pattern
Return Nothing
End Try
Return gridPattern
End Function 'GetGridPattern
///--------------------------------------------------------------------
/// <summary>
/// Set up grid item event listeners.
/// </summary>
/// <param name="targetControl">
/// The grid item container of interest.
/// </param>
///--------------------------------------------------------------------
private void SetGridItemEventListeners(AutomationElement targetControl)
{
AutomationFocusChangedEventHandler gridItemFocusChangedListener =
new AutomationFocusChangedEventHandler(OnGridItemFocusChange);
Automation.AddAutomationFocusChangedEventHandler(
gridItemFocusChangedListener);
}
'''--------------------------------------------------------------------
''' <summary>
''' Set up grid item event listeners.
''' </summary>
''' <param name="targetControl">
''' The grid item container of interest.
''' </param>
'''--------------------------------------------------------------------
Private Sub SetGridItemEventListeners( _
ByVal targetControl As AutomationElement)
Dim gridItemFocusChangedListener As AutomationFocusChangedEventHandler = _
AddressOf OnGridItemFocusChange
Automation.AddAutomationFocusChangedEventHandler( _
gridItemFocusChangedListener)
End Sub
///--------------------------------------------------------------------
/// <summary>
/// Event handler for grid item focus change.
/// Can be used to track traversal of individual grid items
/// within a grid.
/// </summary>
/// <param name="src">Object that raised the event.</param>
/// <param name="e">Event arguments.</param>
///--------------------------------------------------------------------
private void OnGridItemFocusChange(
object src, AutomationFocusChangedEventArgs e)
{
// Make sure the element still exists. Elements such as tooltips
// can disappear before the event is processed.
AutomationElement sourceElement;
try
{
sourceElement = src as AutomationElement;
}
catch (ElementNotAvailableException)
{
return;
}
// Gets a GridItemPattern from the source of the event.
GridItemPattern gridItemPattern =
GetGridItemPattern(sourceElement);
if (gridItemPattern == null)
{
return;
}
// Gets a GridPattern from the grid container.
GridPattern gridPattern =
GetGridPattern(gridItemPattern.Current.ContainingGrid);
if (gridPattern == null)
{
return;
}
AutomationElement gridItem = null;
try
{
gridItem = gridPattern.GetItem(
gridItemPattern.Current.Row,
gridItemPattern.Current.Column);
}
catch (ArgumentOutOfRangeException)
{
// If the requested row coordinate is larger than the RowCount
// or the column coordinate is larger than the ColumnCount.
// -- OR --
// If either of the requested row or column coordinates
// are less than zero.
// TO DO: error handling.
}
// Further event processing can be done at this point.
// For the purposes of this sample we just report item properties.
StringBuilder gridItemReport = new StringBuilder();
gridItemReport.AppendLine(
gridItemPattern.Current.Row.ToString()).AppendLine(
gridItemPattern.Current.Column.ToString()).AppendLine(
gridItemPattern.Current.RowSpan.ToString()).AppendLine(
gridItemPattern.Current.ColumnSpan.ToString()).AppendLine(
gridItem.Current.AutomationId.ToString());
Console.WriteLine(gridItemReport.ToString());
}
///--------------------------------------------------------------------
/// <summary>
/// Handles our application shutdown.
/// </summary>
/// <param name="args">Event arguments.</param>
///--------------------------------------------------------------------
protected override void OnExit(System.Windows.ExitEventArgs args)
{
Automation.RemoveAllEventHandlers();
base.OnExit(args);
}
'''--------------------------------------------------------------------
''' <summary>
''' Event handler for grid item focus change.
''' Can be used to track traversal of individual grid items
''' within a grid.
''' </summary>
''' <param name="src">Object that raised the event.</param>
''' <param name="e">Event arguments.</param>
'''--------------------------------------------------------------------
Private Sub OnGridItemFocusChange( _
ByVal src As Object, ByVal e As AutomationFocusChangedEventArgs)
' Make sure the element still exists. Elements such as tooltips
' can disappear before the event is processed.
Dim sourceElement As AutomationElement
Try
sourceElement = DirectCast(src, AutomationElement)
Catch exc As ElementNotAvailableException
Return
End Try
' Gets a GridItemPattern from the source of the event.
Dim gridItemPattern As GridItemPattern = _
GetGridItemPattern(sourceElement)
If gridItemPattern Is Nothing Then
Return
End If
' Gets a GridPattern from the grid container.
Dim gridPattern As GridPattern = _
GetGridPattern(gridItemPattern.Current.ContainingGrid)
If gridPattern Is Nothing Then
Return
End If
Dim gridItem As AutomationElement = Nothing
Try
gridItem = gridPattern.GetItem( _
gridItemPattern.Current.Row, gridItemPattern.Current.Column)
Catch
' If the requested row coordinate is larger than the RowCount
' or the column coordinate is larger than the ColumnCount.
' -- OR --
' If either of the requested row or column coordinates
' are less than zero.
' TO DO: error handling.
End Try
' Further event processing can be done at this point.
' For the purposes of this sample we just report item properties.
Dim gridItemReport As New StringBuilder()
gridItemReport.AppendLine( _
gridItemPattern.Current.Row.ToString()).AppendLine( _
gridItemPattern.Current.Column.ToString()).AppendLine( _
gridItemPattern.Current.RowSpan.ToString()).AppendLine( _
gridItemPattern.Current.ColumnSpan.ToString()).AppendLine( _
gridItem.Current.AutomationId.ToString())
Console.WriteLine(gridItemReport.ToString())
End Sub
'''--------------------------------------------------------------------
''' <summary>
''' Handles our application shutdown.
''' </summary>
''' <param name="args">Event arguments.</param>
'''--------------------------------------------------------------------
Protected Overrides Sub OnExit(ByVal args As System.Windows.ExitEventArgs)
Automation.RemoveAllEventHandlers()
MyBase.OnExit(args)
End Sub