FrameworkElement.GetVisualChild(Int32) 方法

定义

重写 GetVisualChild(Int32),并从子元素集合中返回指定索引处的子元素。

protected:
 override System::Windows::Media::Visual ^ GetVisualChild(int index);
protected override System.Windows.Media.Visual GetVisualChild (int index);
override this.GetVisualChild : int -> System.Windows.Media.Visual
Protected Overrides Function GetVisualChild (index As Integer) As Visual

参数

index
Int32

集合中请求的子元素的从零开始的索引。

返回

Visual

请求的子元素。 这不应返回 null;如果提供的索引超出范围,则引发异常。

示例

以下示例演示自定义装饰器如何使用它为其多个视觉子级维护的值声明 VisualCollection 的值。 这些值通过重写 VisualChildrenCountGetVisualChild报告。

// To store and manage the adorner's visual children.
VisualCollection visualChildren;
' To store and manage the adorner's visual children.
Private visualChildren As VisualCollection
// 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]; }
' Override the VisualChildrenCount and GetVisualChild properties to interface with 
' the adorner's visual collection.
Protected Overrides ReadOnly Property VisualChildrenCount() As Integer
    Get
        Return visualChildren.Count
    End Get
End Property
Protected Overrides Function GetVisualChild(ByVal index As Integer) As Visual
    Return visualChildren(index)
End Function

注解

在实现中 FrameworkElement ,唯一的有效索引为零。 用于 GetVisualChild 支持零个或一个子元素的内容模型,而不是集合。

继承者说明

此实现仅适用于不维护视觉子元素的任何描述性集合的元素。 具有此类集合的任何元素都必须重写此方法,并将索引映射到该元素支持的子元素集合中的等效索引。 范围中从零到 VisualChildrenCount (的索引应) 返回有效元素;任何其他索引都应引发非范围异常。 支持子集合并重写GetVisualChild(Int32)以返回多个可能的子元素类型的示例。Panel

默认实现 FrameworkElement 假定只有一个视觉子级。 为零以外的任何传递 index 的值都会导致引发异常。 几个常见元素(如修饰器、装饰器或具有专用呈现的元素)将替代 FrameworkElement 从中间基类) 实现的实现 (。 某些实现仍强制实施一个视觉子级,而另一些实现则允许集合。

适用于