FrameworkElement.GetVisualChild(Int32) 方法

定義

覆寫 GetVisualChild(Int32),並從子項目集合傳回指定索引處的子系。

C#
protected override System.Windows.Media.Visual GetVisualChild (int index);

參數

index
Int32

集合中要求的子項目之以零為起始的索引。

傳回

Visual

要求的子項目。 這應該不會傳回 null;如果提供的索引超出範圍,則會擲回例外狀況。

範例

下列範例示範自訂裝飾項如何使用它為其多個視覺子系維護的值所宣告 VisualCollection 的值。 這些值是透過 和 GetVisualChildVisualChildrenCount 覆寫來報告。

C#
// To store and manage the adorner's visual children.
VisualCollection visualChildren;
C#
// Override the VisualChildrenCount and GetVisualChild properties to interface with 
// the adorner's visual collection.
protected override int VisualChildrenCount { get { return visualChildren.Count; } }
protected override Visual GetVisualChild(int index) { return visualChildren[index]; }

備註

在 實作中 FrameworkElement ,唯一有效的索引是零。 的內容模型 GetVisualChild 支援零或一個子專案,而不是集合。

給繼承者的注意事項

這個實作僅適用于不會維護視覺子專案之任何描述性集合的專案。 具有這類集合的任何專案都必須覆寫這個方法,並將索引對應至該元素所支援子專案集合中的對等索引。 範圍從零到 VisualChildrenCount (減一) 的索引應該傳回有效的元素;任何其他索引都應該擲回超出範圍例外狀況。 支援子集合並覆寫 GetVisualChild(Int32) 以傳回多個可能子系的專案類型範例為 Panel

中的預設實作 FrameworkElement 假設只有一個視覺子系。 針對零以外的任何傳遞 index 值,都會擲回例外狀況。 數個常見的元素,例如裝飾專案、裝飾項或具有特製化轉譯的元素,會覆寫 FrameworkElement 實作 (中繼基類的實作) 。 有些實作仍會強制執行一個視覺子系,而其他實作則允許集合。

適用於

產品 版本
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7